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 badges4 Answers
Reset to default 0you 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 "'
" instead of '
(single quotation mark) in your text fields.
本文标签: how can i stop custom fields that have apostrophes from breaking my code
版权声明:本文标题:how can i stop custom fields that have apostrophes from breaking my code 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741315586a2371894.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论