Injecting Custom PHP Functions into Kalin’s Post List


-- Download Injecting Custom PHP Functions into Kalin's Post List as PDF --


In version 3.0 of Kalin’s post List WordPress Plugin, I added a new feature that adds even more customizability, the [php_function] shortcode which you can use to call any user-defined PHP function you have created. In version 3.1 of the plugin, I changed it slightly.

Here’s how to use it.

First, add this line into your wp-config.php file:

define("KALINS_ALLOW_PHP", true);

It will need to go above the line that says “That’s all, stop editing! Happy blogging.” This is for security, just to be on the safe side. I don’t believe there are any major security issues with this, but if I’m wrong, this ensures that no blog will be put at risk without permission.

Next, create your function. I put mine in my wp-config.php file, but you could probably put it in other locations. You can name your function whatever you want, as long as it’s unique. Your function may have one or two parameters. The first one (new in version 3.1) will always be the $post object for the current post in the list. If you can’t get all the necessary information from the $post object, the second parameter will be your optional custom param. If you require more than one custom parameter, you will need to pass it in as a delimited list or JSON string. The parameter will be a string, so if you need numbers or booleans, you will need to run a conversion on them.

Now that your function is ready, you can call it with the shortcode [php_function name="function_name" param=""]. Replace the ‘name’ parameter with the name of the function. Replace the ‘param’ parameter with the function parameter(s).

As of version 3.1, this function no longer runs in a pseudo wordpress loop, so you cannot use the global $post object, but you now use the first parameter of your function, which should be the correct $post object.

The other option for getting post info is to actually pass it in through the ‘param’ parameter by use of a shortcode within the shortcode. For example, if you wanted a function called ‘myFunction’ to receive the post’s author as its parameter, you would use the shortcode: [php_function name=”myFunction” param=”[post_author]“]. The internal shortcode can even have its own parameters if you want. A shortcode with parameters inside a shortcode parameter that all runs inside a shortcode. :)

—————————-

So here is an example of something simple you can do with this feature. If you copy this code into your wp-config.php, it will output an edit link only if the currently logged in user is the author of the post. You can then refer to this function in your post list with the shortcode: [php_function name="postListEditLink"].

define("KALINS_ALLOW_PHP", true);
function postListEditLink($post){
	$user = wp_get_current_user();
	$wpurl = get_blogInfo("wpurl");
	if($post->post_author == $user->ID){
		return '<a href="' .$wpurl .'/wp-admin/post.php?post=' .$post->ID .'&action=edit" >edit</a>';
	}else{
		return "";
	}
}

21 thoughts on “Injecting Custom PHP Functions into Kalin’s Post List”

  1. If I add a param, the function doesn’t process.

    If I call it ‘param’, it won’t save. If I call it ‘parameter name‘ it saves, but the function won’t process and the function shortcode itself is displayed.

  2. I need a little help. Is there someone who can help me by providing code that would allow me to have a 200px by 200px thumbnail for each post using a featured image. my current theme supports this action, but i’m not at all versed in php so any help would be great thanks in advance

    1. I assume you’re talking about using Post List to do this, right? In which case you’d do something like this: <img src="[post_thumb]" width="200" height="200" />
      note that I haven’t tested this and it also assumes your images are already set to 200×200, or else they’ll be stretched to fit.

  3. Hi there,

    I love the flexibility and power of this plugin, but I found a couple of minor issues:

    First, the [post_excerpt] does not play well with UTF-8. It severs double-byte characters, resulting in an unreadable character in the end of the excerpt, just before the ellipsis. For example, “Αθήνα…” becomes “Αθήν�…”

    Secondly, it does not show categories from custom taxonomies, even when you select the custom taxonomy post type in the menu.
    For example, I use All-in-one Event Calendar, which creates a custom taxonomy for its events and categories. Although I can select the custom post type in the Advanced Post List Settings page, the custom categories are not shown in the category list.

    1. Yeah, the custom taxonomy is an issue that I never got around to fixing. I’m not sure about the utf-8 stuff. I’m not totally familiar with the different kinds of text encoding and how they work.

      1. UTF-8 uses an adaptable system with multiple bytes for each letter, which to old software would look like two (or more) individual ASCII characters. So when you truncate the text, you run the risk of separating a two-byte pair, resulting in an unreadable character.

        The reason why you did not notice this issue with latin languages, is because in the latin alphabet only the first byte is used.

        You can find more information here: http://en.wikipedia.org/wiki/Utf-8

        A better approach would be to set the excerpt limit to words, not characters, by counting spaces. This way no words would be truncated, and the UTF-8 would be solved.

      2. Ah, okay thanks, that makes a lot more sense now. I was going to change it eventually to be based on words anyway, but alas, I’ve moved on to other programming projects.

  4. Hi there!

    Thanks for this wonderful plugin. I think it is going to do just what I want. I am running the shortcode on a PAGE and the idea is that it will generate a list of posts in the main body of the page and display them along with a whole bunch of their custom fields. It is mostly working out as I would expect.

    I am running into a snag, however, when I try to incorporate a PHP function. Basically, no matter which Preset I begin with, if I add a php function to the content, I trigger a very weird bug where below the list, it displays the tag list, the title, and the content of the final post in the list. It displays it right inline with the page that is executing the shortcode, using that page’s styles and template.

    Any ideas what is going on? If it would be helpful for me to provide any code, let me know and I’d be glad to.

    1. Hi, thanks for trying the plugin. I’ve looked into the php_function shortcode, and it still seems to be working fine for me. I couldn’t find a way to reproduce what you’re seeing. If you want you can send me any source code and I can try to look into it. Have you tried running a function that has nothing but a string return to make sure it’s not something within the function? you remembered to define KALINS_ALLOW_PHP constant? You’ve formatted the shortcode correctly?

      1. Thanks for much for the reply!

        I did remember to define the constant. I ended up doing in my functions.php file as opposed to my wp-config file, but I tried switching it to the wp-config file with the exact same results. Plus, the function is working fine within the list, except the very last item is also pulling code from the post itself and putting it at the bottom of them page (even below subsequent lists). Also, the list looks fine in the preview in the admin settings screen.

        I have indeed tried a simple (return “hello world”;) function and it is the same issue. It even happens when I add that shortcode to the ‘simplePostList_5′ template.

        What chunks of code would be helpful? I’m happy to send whatever you need, or even screenshots. Perhaps email would be easier than post comments here?

        Thanks again! Looking forward to getting this solved!

      2. Ok. I have a vague theory. It seems like maybe the <?php the_content('Read the rest of this entry »'); ?> code which is in the page template is getting confused by the function and thinking that the last post on the list is also the current page and so it should get that content too…. Does that make any sense?

  5. I’m having issues using the “Require all selected categories” option on custom post pages.

    It seems to work properly on all other pages and posts.

    I’ve created a custom post type called ‘books’ and categories for reviews, reader resources, and title1, title2, etc.

    In my sidebar for a specific title, i want a post list where the categories = review & title1. In other words, I only want reviews for title1 listed.

    Any idea why this wouldn’t be working properly? I’m using version 3.0 of the plugin.

    thanks

    1. After further inspection it works when used on regular Pages but not on Custom Type pages, Post pages or Page of Post.

    2. Thanks so much for pointing this out. I haven’t had a chance to look into it yet but I suspect it was an oversite in my testing since I don’t normally use custom post types. I will try to get on this in the next couple weeks but I can’t make any promises.

      1. kalin,

        if you’d like to see it in action, send me an email and I’ll send you the development url since the site isn’t public yet.

        cheers,

        don…

      2. I finally found some time to look into this, and I can’t seem to reproduce the problem. I have a custom post type post and it shows up in results when I have “require all selected categories” and both of its categories selected, assuming I have either “all” or the post name selected in the Post Type field. If anyone has any more information about how to reproduce this error, that would help greatly.

  6. I was having a problem, here’s how I solved it.

    I have [post_title]s with single quotes in them, and when I ran it through [php_function] I get errors. The shortcode looks like:

    [php_function name=”qt_strip_tags” param=”[post_title]“]

    The function I’m processing it through is simply:


    function qt_strip_tags($text){
    return __(html_entity_decode($text));
    }

    which processes the string through qTranslate’s (multi-language plugin) filters, showing the title in the appropriate language for the page.

    In kalins-post-list.php line 234 I modified it to read:


    $str = str_replace("[post_title]", htmlspecialchars($page->post_title, ENT_QUOTES), $str);

    By adding the flag I was able to process the single quotes titles properly.

    Problem solved.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>