Advanced Custom Fields code displayed as plaintext when written in Code Snippets plugin. How to fix?Wordpress & advanced custom fieldsAdvanced Custom Fields inside a plugindisplaying standard WP get_content with 'Advanced Custom Fields'Advanced Custom Fields and functionsDisplaying content of Advanced Custom FieldsHow to display advanced custom fields with an if statementAdvanced Custom Fields Hidden FieldDisplay Advanced custom fields on a themeWP - Advanced custom field date displayGenerate dynamic link in WordPress
Can a country avoid prosecution for crimes against humanity by denying it happened?
How does Harry wear the invisibility cloak?
Can there be plants on the dark side of a tidally locked world?
Finder/Terminal: Find files that contain less than 21 lines of text
Is it possible to observe space debris with Binoculars?
To which airspace does the border of two adjacent airspaces belong to?
Is torque as fundamental a concept as force?
Why didn't Thatcher give Hong Kong to Taiwan?
Why do we need explainable AI?
In-universe, why does Doc Brown program the time machine to go to 1955?
Which is the best password hashing algorithm in .NET Core?
Do I need to get a noble in order to win Splendor?
First Number to Contain Each Letter
Importance of electrolytic capacitor size
Did Alan Turing's student Robin Gandy assert that Charles Babbage had no notion of a universal computing machine?
What is the difference between "wie" and "nach" in "Klingt wie/nach..."
Is Levitate supposed to basically disable a melee based enemy?
Question about derivation of kinematics equations
Travel to USA with a stuffed puppet
How to describe hit point damage without talking about wounds
Was "The Hobbit" ever abridged?
Has Rey's new lightsaber been seen before in canon or legends?
How to move UVs by a set amount of pixels?
How to encode a class with 24,000 categories?
Advanced Custom Fields code displayed as plaintext when written in Code Snippets plugin. How to fix?
Wordpress & advanced custom fieldsAdvanced Custom Fields inside a plugindisplaying standard WP get_content with 'Advanced Custom Fields'Advanced Custom Fields and functionsDisplaying content of Advanced Custom FieldsHow to display advanced custom fields with an if statementAdvanced Custom Fields Hidden FieldDisplay Advanced custom fields on a themeWP - Advanced custom field date displayGenerate dynamic link in WordPress
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
On Wordpress, I’m trying to add a link to single product page using Code Snippets with Advanced Custom Fields. Instead of a link, my code displays as plaintext.
I have tried this code:
function product_datasheet_below_summary() ?>
$link = get_field('datasheet');
if( $link ):
$link_url = $link['url'];
$link_title = $link['title'];
$link_target = $link['target'] ? $link['target'] : '_self';
?>
<a class="button" href="<?php echo esc_url($link_url); ?>" target="<?php echo esc_attr($link_target); ?>"><?php echo esc_html($link_title); ?></a>
<?php
;
add_action( 'ocean_after_single_product_meta', 'product_datasheet_below_summary', 5 );
This doesn’t work. I was hoping for a link to the Datasheet, but it simply prints, in plaintext:
$link = get_field(‘datasheet’); if( $link ): $link_url =
$link[‘url’]; $link_title = $link[‘title’]; $link_target =
$link[‘target’] ? $link[‘target’] : ‘_self’; ?>
followed by a generic square button link.
What am I doing wrong here? Thanks very much for your help.
Thanks for your advice. Instead of using Code Snippets I just created a child theme and edited the relevant .php file, adding the following:
`
if( $link ):
$link_url = $link['url'];
$link_title = $link['title'];
$link_target = $link['target'] ? $link['target'] : '_self';
?>
<a class= "button" id="datasheet-button" href="<?php echo esc_url($link_url); ?>" target="<?php echo esc_attr($link_target); ?>"><?php echo esc_html($link_title); ?></a>
<?php endif; ?>`
wordpress advanced-custom-fields code-snippets
add a comment |
On Wordpress, I’m trying to add a link to single product page using Code Snippets with Advanced Custom Fields. Instead of a link, my code displays as plaintext.
I have tried this code:
function product_datasheet_below_summary() ?>
$link = get_field('datasheet');
if( $link ):
$link_url = $link['url'];
$link_title = $link['title'];
$link_target = $link['target'] ? $link['target'] : '_self';
?>
<a class="button" href="<?php echo esc_url($link_url); ?>" target="<?php echo esc_attr($link_target); ?>"><?php echo esc_html($link_title); ?></a>
<?php
;
add_action( 'ocean_after_single_product_meta', 'product_datasheet_below_summary', 5 );
This doesn’t work. I was hoping for a link to the Datasheet, but it simply prints, in plaintext:
$link = get_field(‘datasheet’); if( $link ): $link_url =
$link[‘url’]; $link_title = $link[‘title’]; $link_target =
$link[‘target’] ? $link[‘target’] : ‘_self’; ?>
followed by a generic square button link.
What am I doing wrong here? Thanks very much for your help.
Thanks for your advice. Instead of using Code Snippets I just created a child theme and edited the relevant .php file, adding the following:
`
if( $link ):
$link_url = $link['url'];
$link_title = $link['title'];
$link_target = $link['target'] ? $link['target'] : '_self';
?>
<a class= "button" id="datasheet-button" href="<?php echo esc_url($link_url); ?>" target="<?php echo esc_attr($link_target); ?>"><?php echo esc_html($link_title); ?></a>
<?php endif; ?>`
wordpress advanced-custom-fields code-snippets
add a comment |
On Wordpress, I’m trying to add a link to single product page using Code Snippets with Advanced Custom Fields. Instead of a link, my code displays as plaintext.
I have tried this code:
function product_datasheet_below_summary() ?>
$link = get_field('datasheet');
if( $link ):
$link_url = $link['url'];
$link_title = $link['title'];
$link_target = $link['target'] ? $link['target'] : '_self';
?>
<a class="button" href="<?php echo esc_url($link_url); ?>" target="<?php echo esc_attr($link_target); ?>"><?php echo esc_html($link_title); ?></a>
<?php
;
add_action( 'ocean_after_single_product_meta', 'product_datasheet_below_summary', 5 );
This doesn’t work. I was hoping for a link to the Datasheet, but it simply prints, in plaintext:
$link = get_field(‘datasheet’); if( $link ): $link_url =
$link[‘url’]; $link_title = $link[‘title’]; $link_target =
$link[‘target’] ? $link[‘target’] : ‘_self’; ?>
followed by a generic square button link.
What am I doing wrong here? Thanks very much for your help.
Thanks for your advice. Instead of using Code Snippets I just created a child theme and edited the relevant .php file, adding the following:
`
if( $link ):
$link_url = $link['url'];
$link_title = $link['title'];
$link_target = $link['target'] ? $link['target'] : '_self';
?>
<a class= "button" id="datasheet-button" href="<?php echo esc_url($link_url); ?>" target="<?php echo esc_attr($link_target); ?>"><?php echo esc_html($link_title); ?></a>
<?php endif; ?>`
wordpress advanced-custom-fields code-snippets
On Wordpress, I’m trying to add a link to single product page using Code Snippets with Advanced Custom Fields. Instead of a link, my code displays as plaintext.
I have tried this code:
function product_datasheet_below_summary() ?>
$link = get_field('datasheet');
if( $link ):
$link_url = $link['url'];
$link_title = $link['title'];
$link_target = $link['target'] ? $link['target'] : '_self';
?>
<a class="button" href="<?php echo esc_url($link_url); ?>" target="<?php echo esc_attr($link_target); ?>"><?php echo esc_html($link_title); ?></a>
<?php
;
add_action( 'ocean_after_single_product_meta', 'product_datasheet_below_summary', 5 );
This doesn’t work. I was hoping for a link to the Datasheet, but it simply prints, in plaintext:
$link = get_field(‘datasheet’); if( $link ): $link_url =
$link[‘url’]; $link_title = $link[‘title’]; $link_target =
$link[‘target’] ? $link[‘target’] : ‘_self’; ?>
followed by a generic square button link.
What am I doing wrong here? Thanks very much for your help.
Thanks for your advice. Instead of using Code Snippets I just created a child theme and edited the relevant .php file, adding the following:
`
if( $link ):
$link_url = $link['url'];
$link_title = $link['title'];
$link_target = $link['target'] ? $link['target'] : '_self';
?>
<a class= "button" id="datasheet-button" href="<?php echo esc_url($link_url); ?>" target="<?php echo esc_attr($link_target); ?>"><?php echo esc_html($link_title); ?></a>
<?php endif; ?>`
wordpress advanced-custom-fields code-snippets
wordpress advanced-custom-fields code-snippets
edited Mar 29 at 17:36
divinefurnace
asked Mar 28 at 3:21
divinefurnacedivinefurnace
71 silver badge3 bronze badges
71 silver badge3 bronze badges
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You're getting plaintext after the first ?>
because that's a php
ending tag, and the Code Snippets plugin doesn't allow for multiple php
statements and is simply crashing and dumping plain text rather than executing code.
You need to rewrite the whole function as one php
statement and echo
all the button html
, along with the php
variables delimited in the html
with .
's. A simple example:
<?php
$var = "Hello World";
echo "<p>The value of the variable is : " . $var . "</p>";
?>
And you may need to use the more standard ACF get field construct, too:
$value = get_field( "text_field" );
Search SE for more examples of echoing html
in php
.
The use of get_field and using $link["url"] is correct as it means he has the field set up as a link object so he needs to grab the values from the array.
– Daniel Vickers
Mar 28 at 17:37
Thanks for your advice. Instead of using Code Snippets I just created a child theme and edited the relevant .php file, adding the following:<?php $link = get_field('datasheet'); if( $link ): $link_url = $link['url']; $link_title = $link['title']; $link_target = $link['target'] ? $link['target'] : '_self'; ?> <a class= "button" id="datasheet-button" href="<?php echo esc_url($link_url); ?>" target="<?php echo esc_attr($link_target); ?>"><?php echo esc_html($link_title); ?></a> <?php endif; ?>
– divinefurnace
Mar 29 at 17:35
add a comment |
Your function is a bit all over the place, I have cleaned it up to work in the output you want it to using an object instead of echoing out multiple parts of the button code. This in my opinion is easier to manage and looks nicer as it keeps the HTML and PHP code as separate as possible:
function product_datasheet_below_summary()
$link = get_field('datasheet');
if( $link )
$link_url = $link['url'];
$link_title = $link['title'];
$link_target = $link['target'] ? $link['target'] : '_self';
ob_start();?>
<?php if($link):?>
<a class="button" href="<?php echo $link_url;?>" target="<?php echo $link_target;?>"><?php echo $link_title;?></a>
<?php endif;
return ob_get_clean();
add_action( 'ocean_after_single_product_meta', 'product_datasheet_below_summary', 5 );?>
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55389673%2fadvanced-custom-fields-code-displayed-as-plaintext-when-written-in-code-snippets%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You're getting plaintext after the first ?>
because that's a php
ending tag, and the Code Snippets plugin doesn't allow for multiple php
statements and is simply crashing and dumping plain text rather than executing code.
You need to rewrite the whole function as one php
statement and echo
all the button html
, along with the php
variables delimited in the html
with .
's. A simple example:
<?php
$var = "Hello World";
echo "<p>The value of the variable is : " . $var . "</p>";
?>
And you may need to use the more standard ACF get field construct, too:
$value = get_field( "text_field" );
Search SE for more examples of echoing html
in php
.
The use of get_field and using $link["url"] is correct as it means he has the field set up as a link object so he needs to grab the values from the array.
– Daniel Vickers
Mar 28 at 17:37
Thanks for your advice. Instead of using Code Snippets I just created a child theme and edited the relevant .php file, adding the following:<?php $link = get_field('datasheet'); if( $link ): $link_url = $link['url']; $link_title = $link['title']; $link_target = $link['target'] ? $link['target'] : '_self'; ?> <a class= "button" id="datasheet-button" href="<?php echo esc_url($link_url); ?>" target="<?php echo esc_attr($link_target); ?>"><?php echo esc_html($link_title); ?></a> <?php endif; ?>
– divinefurnace
Mar 29 at 17:35
add a comment |
You're getting plaintext after the first ?>
because that's a php
ending tag, and the Code Snippets plugin doesn't allow for multiple php
statements and is simply crashing and dumping plain text rather than executing code.
You need to rewrite the whole function as one php
statement and echo
all the button html
, along with the php
variables delimited in the html
with .
's. A simple example:
<?php
$var = "Hello World";
echo "<p>The value of the variable is : " . $var . "</p>";
?>
And you may need to use the more standard ACF get field construct, too:
$value = get_field( "text_field" );
Search SE for more examples of echoing html
in php
.
The use of get_field and using $link["url"] is correct as it means he has the field set up as a link object so he needs to grab the values from the array.
– Daniel Vickers
Mar 28 at 17:37
Thanks for your advice. Instead of using Code Snippets I just created a child theme and edited the relevant .php file, adding the following:<?php $link = get_field('datasheet'); if( $link ): $link_url = $link['url']; $link_title = $link['title']; $link_target = $link['target'] ? $link['target'] : '_self'; ?> <a class= "button" id="datasheet-button" href="<?php echo esc_url($link_url); ?>" target="<?php echo esc_attr($link_target); ?>"><?php echo esc_html($link_title); ?></a> <?php endif; ?>
– divinefurnace
Mar 29 at 17:35
add a comment |
You're getting plaintext after the first ?>
because that's a php
ending tag, and the Code Snippets plugin doesn't allow for multiple php
statements and is simply crashing and dumping plain text rather than executing code.
You need to rewrite the whole function as one php
statement and echo
all the button html
, along with the php
variables delimited in the html
with .
's. A simple example:
<?php
$var = "Hello World";
echo "<p>The value of the variable is : " . $var . "</p>";
?>
And you may need to use the more standard ACF get field construct, too:
$value = get_field( "text_field" );
Search SE for more examples of echoing html
in php
.
You're getting plaintext after the first ?>
because that's a php
ending tag, and the Code Snippets plugin doesn't allow for multiple php
statements and is simply crashing and dumping plain text rather than executing code.
You need to rewrite the whole function as one php
statement and echo
all the button html
, along with the php
variables delimited in the html
with .
's. A simple example:
<?php
$var = "Hello World";
echo "<p>The value of the variable is : " . $var . "</p>";
?>
And you may need to use the more standard ACF get field construct, too:
$value = get_field( "text_field" );
Search SE for more examples of echoing html
in php
.
answered Mar 28 at 4:05
markratledgemarkratledge
15k10 gold badges50 silver badges96 bronze badges
15k10 gold badges50 silver badges96 bronze badges
The use of get_field and using $link["url"] is correct as it means he has the field set up as a link object so he needs to grab the values from the array.
– Daniel Vickers
Mar 28 at 17:37
Thanks for your advice. Instead of using Code Snippets I just created a child theme and edited the relevant .php file, adding the following:<?php $link = get_field('datasheet'); if( $link ): $link_url = $link['url']; $link_title = $link['title']; $link_target = $link['target'] ? $link['target'] : '_self'; ?> <a class= "button" id="datasheet-button" href="<?php echo esc_url($link_url); ?>" target="<?php echo esc_attr($link_target); ?>"><?php echo esc_html($link_title); ?></a> <?php endif; ?>
– divinefurnace
Mar 29 at 17:35
add a comment |
The use of get_field and using $link["url"] is correct as it means he has the field set up as a link object so he needs to grab the values from the array.
– Daniel Vickers
Mar 28 at 17:37
Thanks for your advice. Instead of using Code Snippets I just created a child theme and edited the relevant .php file, adding the following:<?php $link = get_field('datasheet'); if( $link ): $link_url = $link['url']; $link_title = $link['title']; $link_target = $link['target'] ? $link['target'] : '_self'; ?> <a class= "button" id="datasheet-button" href="<?php echo esc_url($link_url); ?>" target="<?php echo esc_attr($link_target); ?>"><?php echo esc_html($link_title); ?></a> <?php endif; ?>
– divinefurnace
Mar 29 at 17:35
The use of get_field and using $link["url"] is correct as it means he has the field set up as a link object so he needs to grab the values from the array.
– Daniel Vickers
Mar 28 at 17:37
The use of get_field and using $link["url"] is correct as it means he has the field set up as a link object so he needs to grab the values from the array.
– Daniel Vickers
Mar 28 at 17:37
Thanks for your advice. Instead of using Code Snippets I just created a child theme and edited the relevant .php file, adding the following:
<?php $link = get_field('datasheet'); if( $link ): $link_url = $link['url']; $link_title = $link['title']; $link_target = $link['target'] ? $link['target'] : '_self'; ?> <a class= "button" id="datasheet-button" href="<?php echo esc_url($link_url); ?>" target="<?php echo esc_attr($link_target); ?>"><?php echo esc_html($link_title); ?></a> <?php endif; ?>
– divinefurnace
Mar 29 at 17:35
Thanks for your advice. Instead of using Code Snippets I just created a child theme and edited the relevant .php file, adding the following:
<?php $link = get_field('datasheet'); if( $link ): $link_url = $link['url']; $link_title = $link['title']; $link_target = $link['target'] ? $link['target'] : '_self'; ?> <a class= "button" id="datasheet-button" href="<?php echo esc_url($link_url); ?>" target="<?php echo esc_attr($link_target); ?>"><?php echo esc_html($link_title); ?></a> <?php endif; ?>
– divinefurnace
Mar 29 at 17:35
add a comment |
Your function is a bit all over the place, I have cleaned it up to work in the output you want it to using an object instead of echoing out multiple parts of the button code. This in my opinion is easier to manage and looks nicer as it keeps the HTML and PHP code as separate as possible:
function product_datasheet_below_summary()
$link = get_field('datasheet');
if( $link )
$link_url = $link['url'];
$link_title = $link['title'];
$link_target = $link['target'] ? $link['target'] : '_self';
ob_start();?>
<?php if($link):?>
<a class="button" href="<?php echo $link_url;?>" target="<?php echo $link_target;?>"><?php echo $link_title;?></a>
<?php endif;
return ob_get_clean();
add_action( 'ocean_after_single_product_meta', 'product_datasheet_below_summary', 5 );?>
add a comment |
Your function is a bit all over the place, I have cleaned it up to work in the output you want it to using an object instead of echoing out multiple parts of the button code. This in my opinion is easier to manage and looks nicer as it keeps the HTML and PHP code as separate as possible:
function product_datasheet_below_summary()
$link = get_field('datasheet');
if( $link )
$link_url = $link['url'];
$link_title = $link['title'];
$link_target = $link['target'] ? $link['target'] : '_self';
ob_start();?>
<?php if($link):?>
<a class="button" href="<?php echo $link_url;?>" target="<?php echo $link_target;?>"><?php echo $link_title;?></a>
<?php endif;
return ob_get_clean();
add_action( 'ocean_after_single_product_meta', 'product_datasheet_below_summary', 5 );?>
add a comment |
Your function is a bit all over the place, I have cleaned it up to work in the output you want it to using an object instead of echoing out multiple parts of the button code. This in my opinion is easier to manage and looks nicer as it keeps the HTML and PHP code as separate as possible:
function product_datasheet_below_summary()
$link = get_field('datasheet');
if( $link )
$link_url = $link['url'];
$link_title = $link['title'];
$link_target = $link['target'] ? $link['target'] : '_self';
ob_start();?>
<?php if($link):?>
<a class="button" href="<?php echo $link_url;?>" target="<?php echo $link_target;?>"><?php echo $link_title;?></a>
<?php endif;
return ob_get_clean();
add_action( 'ocean_after_single_product_meta', 'product_datasheet_below_summary', 5 );?>
Your function is a bit all over the place, I have cleaned it up to work in the output you want it to using an object instead of echoing out multiple parts of the button code. This in my opinion is easier to manage and looks nicer as it keeps the HTML and PHP code as separate as possible:
function product_datasheet_below_summary()
$link = get_field('datasheet');
if( $link )
$link_url = $link['url'];
$link_title = $link['title'];
$link_target = $link['target'] ? $link['target'] : '_self';
ob_start();?>
<?php if($link):?>
<a class="button" href="<?php echo $link_url;?>" target="<?php echo $link_target;?>"><?php echo $link_title;?></a>
<?php endif;
return ob_get_clean();
add_action( 'ocean_after_single_product_meta', 'product_datasheet_below_summary', 5 );?>
answered Mar 28 at 17:36
Daniel VickersDaniel Vickers
6441 gold badge3 silver badges18 bronze badges
6441 gold badge3 silver badges18 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55389673%2fadvanced-custom-fields-code-displayed-as-plaintext-when-written-in-code-snippets%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown