• Resolved csogi

    (@csogi)


    I’d like to add shortcodes to multiple places in one page but be able to link to anchored sections of the page. Will be used on rankings. Is it possible to add an anchor (#1) to the base url so people can share to a specific section of a page?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Robin Cornett

    (@littlerchicken)

    I think that’s theoretically possible, although the code looks a bit funny. Here’s something to get you started:

    add_filter( 'shortcode_atts_scriptless', 'prefix_modify_scriptless_shortcode', 10, 3 );
    /**
     * Allows the use of a new "anchor" attribute on the Scriptless shortcode.
     * If present, will add #[anchor] to the end of the permalink using the
     * scriptless_get_permalink filter.
     *
     * @param array $attributes
     * @param array $pairs
     * @param array $new_attributes
     * @return array
     */
    function prefix_modify_scriptless_shortcode( $attributes, $pairs, $new_attributes ) {
    	add_filter(
    		'scriptlesssocialsharing_get_permalink',
    		function ( $permalink ) use ( $new_attributes ) {
    			if ( ! empty( $new_attributes['anchor'] ) ) {
    				return $permalink . '#' . $new_attributes['anchor'];
    			}
    
    			return $permalink;
    		}
    	);
    
    	return $attributes;
    }
    

    Basically, it’s two steps: the first filter allows you to define/add a new shortcode attribute for an anchor, so you could use [scriptless anchor="point1"], where #point1 would be the ID you are targeting. Then within that filter, you’re passing the new anchor attribute on to the plugin’s permalink filter and just appending the anchor to the link, if it’s set.

    This code was working for me locally, but with very limited testing, so you would want to probably bang on it a lot more and see how it goes.

    Thread Starter csogi

    (@csogi)

    Thank you. I’ll be sure to test it out today. Will let you know.

    Thread Starter csogi

    (@csogi)

    So the only thing that I see now is that when you have more than one anchor on the page as I do, the url that is shared includes all of the anchors rather than just the one in the shortcode.

    %2Frankings%2Fonline-masters-philosophy%2F%23biola%23ciis

    The two I have are: %23biola and %23ciis

    Ideas?

    Plugin Author Robin Cornett

    (@littlerchicken)

    I would experiment with adding code to remove anything from the # and after from the $permalink variable, which should basically “reset” the permalink for each instance of the shortcode. I found a tutorial with some examples which may be helpful.

    Thread Starter csogi

    (@csogi)

    Thank you. I was able to get exactly what I needed. I am also curious if I’d be able to add something like “Copy link to this entry…” below the buttons for users. Is this available to do within the functions or would i need to tweak the plugin itself?

    Thanks

    Plugin Author Robin Cornett

    (@littlerchicken)

    You would need to add your own button, as that kind of functionality requires JavaScript. It should be possible to do with the existing plugin filters (other users have asked about adding JS and it’s possible to do so–check those threads and that may help with a starting point), which would be better than modifying the plugin, so that your modifications are not lost when the plugin updates.

    Thread Starter csogi

    (@csogi)

    Great thank you.

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Add anchor to shortcode?’ is closed to new replies.