0

I want to add some PHP code (to read out URL variables via get_query_var) in the content area of a Wordpress page. To do that I use the following function in my functions.php

function php_execute($html){
    if(strpos($html,"<"."?php")!==false){ ob_start(); eval("?".">".$html);
    $html=ob_get_contents();
    ob_end_clean();
}
return $html;
}
add_filter('the_content','php_execute',100);

With this, I'm able to read out a URL variables of my URL /thank-you/?order_id=ABCDE with <?php echo get_query_var( 'order_id' );?>.

Now I want to add this order ID to a URL (in order to prefill a field in a survey) and tried to add it to the corresponding URL like so:

<iframe src="https://XXX.wufoo.com/embed/YYYYY/def/field414=<?php echo get_query_var( 'order_id' );?>"> <a href="https://XXX.wufoo.com/forms/YYYYY/def/field414=<?php echo get_query_var( 'order_id' );?>">Link to survey</a>

Unfortunately the resulting source code looks like this:

<iframe src="https://XXX.wufoo.com/embed/YYYYY/def/field414=ABCDE &#8222;> <a href="https://XXX.wufoo.com/forms/YYYYY/def/field414=ABCDE &#8222;>">Link to survey</a>

So instead of field414=ABCDE"> it says field414=ABCDE &#8222;>

I'm very new to PHP and think that there might be a problem in the function but can't figure it out.

Does anyone see a mistake somewhere?

Thanks, Patrick

Patrick
  • 65
  • 5
  • is the URL inside the content or are we talking about the page url in the address bar? – GBWDev Jan 30 '20 at 10:00
  • Hint: https://stackoverflow.com/questions/657643/how-to-remove-html-special-chars – mitkosoft Jan 30 '20 at 10:01
  • you need to make it shortcode using function.php file – Neal Developer Jan 30 '20 at 10:03
  • @GBWDEV /thank-you/?order_id=ABCDE is the page URL. Within this page I want to create this iframe which includes the survey. – Patrick Jan 30 '20 at 16:09
  • @Neal Developer you can use shortcode within in the HTML tag. If you mean to create a shortcode for this HTML part I think that's way overkill as I only want to use this code on this particular page and shortcodes should be reserved for s.th. you use more than once. – Patrick Jan 30 '20 at 16:10

1 Answers1

0

Setup correctly iframe tag. Then change you php_execute functions to this.

add_filter( 'the_content', 'php_execute' );
function php_execute( $content )
{
    $orderId = isset($_GET['order_id']) ? trim($_GET['order_id']) : false;

    if ($orderId) {
        $content .= '<iframe src="https://XXX.wufoo.com/embed/YYYYY/def/field414=' . $orderId . '">';
        $content .= '<a href="https://XXX.wufoo.com/forms/YYYYY/def/field414=' . $orderId . '">Link to survey</a>';
    }

    return $content;
}
  • Thanks for the reply. Unfortunately I don't want to add 12 lines of code to my functions.php for s.th. I only plan to use once. The functions.php should be reserved for functions that are used more than one time. – Patrick Jan 30 '20 at 16:11