Support

Account

Home Forums Bug Reports Broken Page Link when layered on top of text

Solved

Broken Page Link when layered on top of text

  • I want to link plain text with a Page Link type field, but when I do, the url breaks (it leaves off the colon of “https://”).

    I type “whatever I’m linking” on my Page, edit my example_page_link Field to point to a page on my site. In the block editor I highlight “whatever I’m linking” and click the chain icon to add a link, then type in the shortcode “[acf field=example_page_link]” in the address section.

    “whatever I’m linking” DOES become hyperlinked on the live page that’s rendered, but when I click the link, the page appears broken because the colon of the “https://” is missing (I get “https//…”). If I just put the raw shortcode on the page [acf field=example_page_link], the entire link renders as text, is itself hyperlinked, and works properly.

    Why won’t hyperlinking work? Bug?

  • Shortcodes cannot be used as attribute values of html elements in WP and they cannot be used in the value of a WP link url in when adding a link.

  • Following the example in this issue:
    https://wordpress.stackexchange.com/questions/291524/inserting-html-tag-with-acf-into-shortcode

    I installed the (free) plugin “Code Snippets” on my WordPress.com site:
    https://wordpress.org/plugins/code-snippets/

    I created a new snippet.

    The code snippet below creates a new shortcode called “[my_special_link]” that takes two parameters:
    1. acf_link_field : the ACF field name you want to call on (assuming it’s a link type field)
    2. text_value : the text you want to hyperlink on top of

    [my_special_link acf_link_field="example_page_link" text_value="Whatever I want"]

    This adds the text “Whatever I want” to the page and hyperlinks it to the ACF field link at field=example_page_link.

    You could adapt this code to chain together however many ACF fields you wanted.

    . . .

    Title: ACF Link Field PLUS Text Value

    Code:

    <?php 
    function my_shortcode( $atts ) {
        $atts = shortcode_atts( array(
            'acf_link_field' => '', // Default value.
        	'text_value' => '', // Default value.
    	), $atts );
    
        $output = '[acf field=' . $atts['acf_link_field'] . ']' ;
        $output = do_shortcode( $output );
        $output = '<a href="' . $output . '">' . $atts['text_value'] . '</a>';
        return $output;
    }
    
    add_shortcode('my_special_link', 'my_shortcode');
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.