• I’ve been searching the Plugins & just haven’t been able to locate what I need (yet), perhaps someone can point me in the right direction.

    I use a tracking solution that allows me to track visitors from search term through to conversion at the keyword level. However, I’ve got to jump through a couple of hoops.

    To enable this solution, I use PHP-Exec, disable the visual editor, and add a little snippet of PHP code to the top of every post/page content entry (no, it doesn’t work if I add it into the template).

    I’d like to know if there’s a plugin/hack that will let me specify a snippet of code to appear ‘automagically’ at the top of every post before I begin adding content?

    OR, perhaps a button I can add to the editor that will insert the code wherever my cursor lies when I push said button?

    Any suggestions would be helpful

    Thanks,
    Ron Jones

Viewing 8 replies - 1 through 8 (of 8 total)
  • So, I’m guessing adding it to the header.php, or page.php of the template doesn’t work? That’s what I did with a javascript function that I needed to be able available to all pages/posts was to add it to the header.php file.

    Maybe you could use filters.

    Thread Starter Ron Jones

    (@jonesre)

    Noooo…that would be too easy!

    It’s a snippet of PHP code that I insert for Xtreme Conversions. I’ve tried it in the header, I’ve tried it just after the body tag…I’ve tried it after the start of “the loop.” I’ve even tried putting it within the same PHP tag and just before the_content, like so:

    <?php error_reporting(0); $cookiename = 'xconversions'; if (isset($_COOKIE[$cookiename])) $var = $_COOKIE[$cookiename]; the_content('<p class="serif">Read the rest of this entry &raquo;</p>'); ?>

    But still no luck. Apparently, the only way it works (and I have tried this) is to past it into the content entry box. And so I need to install Exec-PHP, and turn off the visual editor.

    Ultimately, I need the following code entered at the top of every post/page, etc:

    <?php error_reporting(0); $cookiename = 'xconversions'; if (isset($_COOKIE[$cookiename])) $var = $_COOKIE[$cookiename]; ?>

    I’d like to find a simple way to either click a button and have it past the code for me…OR…find a plugin that will automatically enter my php code at the top of my content entry box.

    Thread Starter Ron Jones

    (@jonesre)

    @renato_s

    Thanks for the link to ‘filters,’ it looks promising. Though I’m not well versed in PHP, I might could hunt & peck my way through it.

    Do you know if there is such a thing as a “plugin blank?” I don’t know if I’m using the right terminology, but what I’ve got in mind is a plugin that is not a plugin, but a framework on which you can build a plugin.

    Sort of a “wireframe plugin”

    Hi!
    Try this code out and tell me if it worked. I’m not a php programmer either, but maybe it will help you… I’ve never made a filter function myself, so I’m not sure how to do it, and it most certainly won’t work as it is. But hopefully will help you get started… The documentation on filters is not so good…

    <?php
    /*
    Plugin Name: Name
    Plugin URI: https://yoursite.com/plugin
    Description: What does it do?
    Version: 0.1
    Author: Your name
    Author URI: https://https://yoursite.com/
    */
    
    function add_script($content) {
        $content='put here your script'.$content;
        return $content;
    }
    
    add_filter('content_save_pre', 'add_script');
    ?>

    If you just need to show the script on the html code, maybe you should use the filter “the_content” instead of “content_save_pre”, as this last one would insert the script in the database along with the content.

    That’s weird.. if I edit single.php and change the code around the_content:

    <h2><?php the_title(); ?></h2>
    
    			<div class="entry">
    				<?php echo 'where does this show?</ br>'; the_content('<p class="serif">Read the rest of this entry &raquo;
    '); ?>

    I see my echo statement right after the title of the post and before the content of the post. From the browser’s perspective, that’s the same as it being part of the post content, within the editor box.

    From the server’s perspective it may not be exactly the same… might have to find the PHP file that has the actual the_content function in it and add that code within it. Looks like it’s in the post-template.php file. get_the_content looks like the function that actually gets the post content itself.

    From:

    function the_content($more_link_text = null, $stripteaser = 0, $more_file = '') {
    	$content = get_the_content($more_link_text, $stripteaser, $more_file);
    	$content = apply_filters('the_content', $content);
    	$content = str_replace(']]>', ']]>', $content);
    	echo $content;
    }

    To:

    function the_content($more_link_text = null, $stripteaser = 0, $more_file = '') {
    	error_reporting(0);
    	$cookiename = 'xconversions';
    	if (isset($_COOKIE[$cookiename])) $var = $_COOKIE[$cookiename];
    	$content = get_the_content($more_link_text, $stripteaser, $more_file);
    	$content = apply_filters('the_content', $content);
    	$content = str_replace(']]>', ']]>', $content);
    	echo $content;
    }

    I don’t know.. just guessing. Depending on the scope that $var should be, might need to add the statement global $var; as the first line within the function.

    Have you tried looking in this thread?

    https://forum.semiologic.com/discussion/1524/#Item_3

    I didn’t read it all the way, but they seem to be discussing your exact challenge.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘How to automatically add php code to the top of each post???’ is closed to new replies.