admin管理员组

文章数量:1287608

I have this code in my php file that creates a button labeled from a field called speaker_file_label and links to a pdf file based on the url entered in the speker_file_url. The fields are created by ACF

    echo do_shortcode(  "[av_button label='".get_sub_field('speaker_file_label')."' link='manually,".get_sub_field ('speaker_file_url')."' link_target='_blank' size='medium' position='left' color='theme-color']" ).'<br>';

This works perfectly when someone just enters text. However if they say somehting like "joe's files" (without quotes) Then the shortcode breaks and the label just becomes "click"

How can i make wordpress not interpret that apostrophe as part of code?

I have this code in my php file that creates a button labeled from a field called speaker_file_label and links to a pdf file based on the url entered in the speker_file_url. The fields are created by ACF

    echo do_shortcode(  "[av_button label='".get_sub_field('speaker_file_label')."' link='manually,".get_sub_field ('speaker_file_url')."' link_target='_blank' size='medium' position='left' color='theme-color']" ).'<br>';

This works perfectly when someone just enters text. However if they say somehting like "joe's files" (without quotes) Then the shortcode breaks and the label just becomes "click"

How can i make wordpress not interpret that apostrophe as part of code?

Share Improve this question asked Nov 3, 2016 at 20:21 presswordpressword 3801 gold badge4 silver badges13 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 0

you could apply the content filters:

    echo do_shortcode(  "[av_button label='".apply_filters('the_content',(get_sub_field('speaker_file_label'))."' link='manually,".get_sub_field ('speaker_file_url')."' link_target='_blank' size='medium' position='left' color='theme-color']" ).'<br>';

the above added pp marks <p></p>

try this:

    echo do_shortcode(  "[av_button label='".wptexturize( get_sub_field('speaker_file_label'))."' link='manually,".get_sub_field ('speaker_file_url')."' link_target='_blank' size='medium' position='left' color='theme-color']" ).'<br>';

Wrap user input in esc_html().

Using your example:

    echo do_shortcode( "[av_button 
        label='" . esc_html( get_sub_field( 'speaker_file_label' ) ) . "' 
        link='manually," . esc_html( get_sub_field ( 'speaker_file_url' ) )."' 
        link_target='_blank'
        size='medium' position='left' color='theme-color']" ).'<br>';

You can make it more readable:

$shortcode = sprintf( "[av_button label='%1$s' link='manually,%2$s' link_target='_blank' size='medium' position='left' color='theme-color']"
    , esc_html( get_sub_field( 'speaker_file_label' ) )
    , esc_html( get_sub_field( 'speaker_file_url' ) )
);

echo do_shortcode( $shortcode ) . '<br>';

Two ways to fix it, would be to invert your quotes from the outset (replace all double quotes (") with single quotes (') and vice versa.

echo do_shortcode(  '[av_button label="' . get_sub_field('speaker_file_label') . '" link="manually,' . get_sub_field ('speaker_file_url') . '" link_target="_blank" size="medium" position="left" color="theme-color"]' ).'<br>';

But that won't stop if people use double quotes ("), so you can escape them, by using addslashes

echo do_shortcode(  '[av_button label="' . addslashes( get_sub_field('speaker_file_label') ) . '" link="manually,' . addslashes( get_sub_field ('speaker_file_url') ) . '" link_target="_blank" size="medium" position="left" color="theme-color"]' ) . '<br>';

This should fix most issues, the first one might fix them all if you don't expect people to use double quotes. I haven't tested either, so let me know if they worked for you or not.

You can write "&#39;" instead of ' (single quotation mark) in your text fields.

本文标签: how can i stop custom fields that have apostrophes from breaking my code