0

In a file that I will call file-a.php I have created a shortcode which prints the following (simplified) HTML markup:

<div><a href="#"><span>Link text</span></a></div>

In file-b.php I recover the [shortcode] and put it into a javascript variable:

var myvariable = "<?php print do_shortcode('[shortcode]'); ?>";

This is the HTML markup which I get when I print "myvariable":

<div><a href="#"><span>Link text

In practice, when the first "/" is detected the markup is interrupted.

I have tried consulting several resources but have not found any solution yet. Please consider that my PHP and JS knowledge is circumscribed. Thank you if you can point me to a possible solution.

5
  • Hi there, could you show the code for do_shortcode function please? Commented Dec 6, 2018 at 11:56
  • If I'm not mistaken, using blackslashes will mask the and escape special characters. Have you tried to do it like that? (e.g.: "something/else" => "something\/else") Commented Dec 6, 2018 at 12:01
  • Yes, I also tried using backslashes to escape forward slashes, with no success. Commented Dec 6, 2018 at 14:24
  • To be more clear, putting a backslash in the basic code... print '<p>Test<\/p>';... renders... var myvariable = <p>Test<\/p>""; Commented Dec 6, 2018 at 14:27
  • My own comment has given me the hint for the solution. I am writing the details here below in some minutes. Commented Dec 6, 2018 at 14:58

3 Answers 3

1

You must encode the value returned by do_shortcode() using the rules of the target language (JavaScript).

The best PHP way to encode a string to generate JavaScript code is json_encode().

Your code should be:

var myvariable = <?php print json_encode(do_shortcode('[shortcode]')); ?>;

The quotes surrounding the string are not needed any more. If given a string, json_encode() produces a representation of the input string that is a valid JavaScript string source code.

For example, if do_shortcode('[shortcode]') returns <div><a href="#"><span>Link text</span></a></div> then the line of code above produces:

var myvariable = "<div><a href=\"#\"><span>Link text<\/span><\/a><\/div>"

which is valid JavaScript code that puts the aforementioned string into the JavaScript variable myvariable.

Sign up to request clarification or add additional context in comments.

Comments

0

Thank you for your answers. This is the code in file-a.php (functions.php, WordPress):

function shortcode_function() { 
    print '<p>Test</p>'; 
}
add_shortcode( 'shortcode', 'shortcode_function' );

This is the code in file-b.php (footer.php, WordPress):

<script>
    jQuery(document).ready(function($) {
    var myvariable = "<?php print do_shortcode('[shortcode]'); ?>";
    $( '.footer' ).prepend( myvariable );
    });
</script>

As you can see this returns a broken < p > tag.

<script>
        jQuery(document).ready(function($) {
        var myvariable = "<p>Test";
        $( '.footer' ).prepend( 'myvariable );
        });
    </script>
</body>
</html>

This is the result with json_encode(), with and without quotes, and nothing is visible on the public page:

var myvariable = "<?php print json_encode(do_shortcode('[shortcode]')); ?>";
var myvariable = "<p>Test""";

var myvariable = <?php print json_encode(do_shortcode('[shortcode]')); ?>;
var myvariable = <p>Test"";

In practice, I am trying to print some dynamic data before the footer area, data extracted from the DB through PHP queries. Dynamic data is correctly printed, but with an incorrect HTML markup.

Comments

0

This is my solution. It works in its basic form, and now I am expanding it.

I have escaped slashes with a backslash in functions.php>

print "<p class='red'>RED<\/p>";

This returns the following code in footer.php:

var str = "<p class='red'>RED<\/p>";

All I have done is sanitizing the string, like so:

var sanitized_str = str.replace(/\\/g, '');

The Javascript variable...

sanitized_str

...is renderered properly, with a correct HTML markup visible online when it is printed like so:

$( '.footer' ).prepend( sanitized_str );

All tags are improperly closed in the page markup (ex. </p> but the public outcome is sanitized and clearly visible.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.