• Resolved cameldance

    (@cameldance)


    I’m using a PHP function that dynamically displays what phone number a website visitor should call based on whether or not a cookie has been set by the visitor arriving at the site through an affiliate link. The function is working fine outside of page and post content areas, such as in header.php and in an advanced text widget in the sidebar.

    Inside the content area however it produces nothing. All the rest of the content before and after where the phone number should be is there, but the phone number is nowhere to be seen. I have installed the plugin “Allow PHP in Posts and Pages” for inserting my PHP code into the page/post content area. Here is what I’m using inside the content area to call my function:

    [php]<?php display_phone_number (); ?>[/php]

    The [php] shortcode is what the plugin calls for to insert php code into the content area.

    Here is the function display_phone_number that is working fine elsewhere on the site:

    function display_phone_number () {<br />
    if(!isset($_COOKIE['ap_id'])) {<br />
        echo "(800) 123-4567";<br />
    } else {<br />
        echo do_shortcode( '[wp_affiliate_referrer_details info="phone"]' );<br />
    }<br />
    }

    I suspect the problem may be that I’m already having to use a shortcode [php][/php] to insert the PHP, and then the actual PHP I’m inserting calls a function that incorporates another shortcode.

    Here is the function that is called by the shortcode [wp_affiliate_referrer_details info="phone"]:

    function wp_affiliate_referrer_details_handler($atts){<br />
        extract(shortcode_atts(array(<br />
            'ap_id' => '',<br />
            'info' => '',<br />
        ), $atts));<br />
        if(empty($ap_id)){//Set it to current referrer<br />
            $ap_id = wp_affiliate_get_referrer();<br />
        }<br />
        if(empty($ap_id)){<br />
            return AFF_NONE;<br />
        }<br />
        return wp_aff_get_affiliate_details($ap_id,$info);<br />
    }

    If I knew how to incorporate into my function display_phone_number the actual code for the function wp_affiliate_referrer_details_handler and tell it that I want to look up “phone”, rather than using the following…

    do_shortcode( '[wp_affiliate_referrer_details info="phone"]' )

    …then I would do so and see if that solved the problem, but I don’t know how. Does anyone know how I can make this work inside my page/post content area just like it’s already working outside of the content area?

    Thanks!

Viewing 9 replies - 1 through 9 (of 9 total)
  • Anyone attempting to do above should ensure they comply (worldwide, if international) with such use of cookies, then review a basic part of any wordpress theme, namely Page Templates. Please review the Codex for more on this.

    Also, I find most of the code you provided to not work as is.

    Thread Starter cameldance

    (@cameldance)

    Is anyone able to provide more specific help? We’re assuming for these purposes that the cookies we’re using are fine, and I have a pretty good handle on page templates. If this issue relates to the template I guess I’m not seeing how.

    I can verify that the code provided does work as-is everywhere outside of the content area of pages and posts (while allowing that there may be another piece to the code that’s part of making it work that I haven’t copied and pasted here), but when I try to bring it inside the content area, i.e. enter it in the page editor so it’s part of the main, editable content of any page, it fails to work.

    I’m assuming that this issue has something to do with the fact that, by default, raw php code entered into the content area of pages doesn’t work.

    Using cookies is completely fine and there is no law to comply or conform to. Cookies are a fundamental aspect of browsing the internet.

    I am not to sure what plugin you are using for the [php] but I am not seeing any obvious reason why it would not work with a shortcode inside a shortcode since the function being called is technically wrapped in a function and rendered before it is returned back into your initial shortcode (confusing).

    Here is what the shortcode would look like without the second level you mentioned (completely untested):

    function display_phone_number () {
    if(!isset($_COOKIE['ap_id'])) {
        echo "(800) 123-4567";
    } else {
       $ap_id = wp_affiliate_get_referrer();
       if(empty($ap_id)){
            return AFF_NONE;
        }
        echo wp_aff_get_affiliate_details($ap_id,'phone');
    }
    }

    Where is the code located that is creating the cookie?

    Using cookies is complexity fine and there is no law to comply or conform to. Cookies are a fundamental aspect of browsing the internet.

    Yes there is when one reads what I wrote and understands it.

    Yes there is when one reads what I wrote and understands it.

    Are you referring to the theme guidelines? If so, it only applies for themes being submited to the .org repo. Not to people customizing the theme once installed.

    Thread Starter cameldance

    (@cameldance)

    Thanks for chiming in @justin! The plugin I’m using for the [php] shortcode is called Allow PHP in Posts and Pages.

    The code that’s creating the cookie is located in the plugin folder for the WordPress Affiliate Platform plugin.

    I think the code you provided is what I didn’t know how to do…figure out what would be the actual straight up PHP code if I weren’t using a shortcode.

    I’ll give it a shot and report back what happens.

    Thanks!

    Thread Starter cameldance

    (@cameldance)

    Well, I got this solved, and it was a very dumb mistake on my part. With the [php] shortcode plugin I was using, the shortcode serves as the opening and closing <?php and ?> tags, so those tags didn’t need to be included. I was placing those tags inside the shortcode, which was breaking it.

    @justin, your code does work though. I am now using it in place of my code that was calling the shortcode inside my function.

    Thanks for the help!

    I wanted to update you on the cookie matter. In EU there is a thing that you must disclose why you are using cookies. It was brought to my attention by another mod here on the forums. If you live in the EU, this applies to you, if not, don’t worry about it.

    @justin,

    Thank for the clarification and confirmation.

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘Insert PHP in Content Area When PHP Includes a Shortcode’ is closed to new replies.