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.
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 »</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.
@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: http://yoursite.com/plugin
Description: What does it do?
Version: 0.1
Author: Your name
Author URI: http://http://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 »
'); ?>
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?
http://forum.semiologic.com/discussion/1524/#Item_3
I didn’t read it all the way, but they seem to be discussing your exact challenge.