As promised in my first post reporting entry, this is a follow up article. I’m going to describe how to easily edit your theme template to insert PHP code rather than using shortcodes, well, mostly. I’m also going to show how to achieve the same effect for single posts by using a sidebar widget.
If you’ve stumbled across this article in a search, go to the first entry, read that, and then come back here.
In the first article I showed how to set up post reporting using Contact Form 7 and Simple Content Reveal. The method used was to enter shortcode from the two plugins into the WordPress editor. It’s easy enough, but if you were to use post reporting on every article it’d either become tedious or you’d often forget to enter the shortcodes. Also, if each article on a page of articles had the shortcode entries, they’d all have the same reveal ID.
Such as here:
[reveal heading="<h4>%image%Report a change in this entry</h4>" id="id2"]
id="id2" would be replicated on the page several times and the result would be that each click of the reveal text would only open the first instance of
id="id2". So what’s required is dynamic id allocation and the way to do that is to edit your templates. Don’t worry though, it’s painless.
Over at my testing site I’m running the default WordPress theme. The WordPress loop is contained in a file called loop.php but I’ve copied that file to a new file called loop-index.php. WordPress will use this file rather than the original and it’s probably safer to work on a copy. If you don’t use TwentyTen, you’ll have to look for the file that contains your loop.
Here’s the code I’ve entered into the template. The first and last lines are from the template itself, the middle three are my additions. You’ll notice that I’m using
do_shortcode() on the middle line, the one that contains the Contact Form details. This is because you can’t enter Contact Form 7 as PHP into your templates so it has to be wrapped in a
You’ll also notice that in order to aid styling, I’ve wrapped the first
[reveal] in a span and given it a class name.
< ?php simple_content_reveal("%image% Report change in information",get_the_ID ()); ?> < ?php echo do_shortcode('[contact-form 2 "Post Reporting"]'); ?> < ?php simple_content_reveal(); ?>
The tricky part of the new method is using
get_the_ID() to define the id of the
[reveal]. What happens is that it picks up the ID of the WordPress post and uses that as the id of the
[reveal]. That way each id will be unique and there will be no conflicts with other iterances of
[reveal] on the same page.
There’s nothing stopping you adding other
[reveal]s on a page or post by using shortcodes in the WordPress editor, you just have to make sure you give each one a new ID.
I’ve managed to put all the content reveal/contact stuff into a sidebar widget. I’ve achieved that by using the Widget Logic plugin. This clever little plugin allows you to define where your widget appears, and where it doesn’t. For this purpose, I only want the widget appearing on single pages. If it appeared alongside multiple posts, there’d be no way of telling which entry was being reported. If that makes sense.
I’ve used shortcodes again here buts to use them in widgets, you have to place this entry in your
function.php. It lives in your theme folder with all the other files. It’s a simple addition on a new line:
add_filter('widget_text', 'do_shortcode', 11);
Once you’ve edited
functions.php and installed Widget Logic, create a Text Widget in your widgetized area, such as your sidebar. Mine looks like this:
At the bottom of the widget, you’ll see the Widget Logic entry.
is_single() will make the widget appear only on single pages.
In order to create a Contact Form 7 in the sidebar I used a new form so that I could define column widths etc. Here’s what the sidebar version looks like, You’ll see that it uses features from the Dynamic Text Extension plugin that we installed in the last article.
What I’ve tried to achieve in this and the previous post is a method to implement post reporting in WordPress posts. It strikes me that it could all be bundled into one plugin (Such as the comment reporting plugin) but after a small amount of time, the effect achieved is quite satisfactory.
If you have any questions or want any help. Leave a comment below.