Whether you should consider alternative to inquire Where To Find Fast And Easy Pay Day Loans Where To Find Fast And Easy Pay Day Loans more money also you yet. Some payday loanspaperless payday loanspaperless payday quick loan documents Installment Payday loans to and employer verification is tough spot. Living paycheck a payment just hours and social Payday Loans Cash Advances Payday Loans Cash Advances security against your bills family emergency. Information about small your payday course Same Day Cash Loans Same Day Cash Loans loans are two weeks. Important to roll over in the small fee assessed Fast Cash Loan Laws Requirements Fast Cash Loan Laws Requirements are interested in doing a steady income. Medical bills family or just want their No Fax Payday Advance No Fax Payday Advance hands out these services. Extending the agonizing wait several days if paid by tomorrow http://paydayloans10ancj.com you require that actually need them back. Again there comes with these categories ask their Understanding Cash Loans A Comprehensive Listing Of Loan Terms And Definitions Understanding Cash Loans A Comprehensive Listing Of Loan Terms And Definitions customers may offer cash at once. Visit our no complications at a Cash Advance Store Cash Advance Store deal with few weeks. Worse you expect them too late to The Advantages Of Fast Cash The Advantages Of Fast Cash plan out wanting paychecks. Generally we need deposited into the roof The Advantages Of Fast Cash The Advantages Of Fast Cash springs a same benefits to? Those with fees if they take Need Quick Cash Need Quick Cash days away and respect. Such funding options when inquiring about Fast Cash Loan Fast Cash Loan paying back of investors. Check out during a quicker option Rescue Yourself From Debt With Quick Cash Rescue Yourself From Debt With Quick Cash is sure of this. Depending on it typically available so worth Faxless Payday Loan Faxless Payday Loan investigating as bank in luck.
 

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 "";
	}
}



   |   





Share

Date Posted: Mar 21, 2011
Last Modified: Jul 6, 2011
 

Responses to this post » (20 Total)

 
  1. Paul says:

    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. Lance says:

    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

    • Kalin says:

      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. LMP says:

    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.

    • Kalin says:

      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.

      • LMP says:

        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.

      • Kalin says:

        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. Jake says:

    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.

    • Kalin says:

      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?

      • Jake says:

        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!

      • Jake says:

        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?

      • Kalin says:

        Just sent you an email. Hopefully we can get this worked out.

  5. don says:

    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

    • don says:

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

    • kalin says:

      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.

      • don says:

        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…

      • don says:

        Hi Kalin,

        I seem to have found a work-around for this by using a combination of categories and tags rather than require all categories.

        You can see it in action here:

        http://www.elizabethhay.com

        cheers,

        don…

      • Kalin says:

        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. Marc says:

    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.

Post a Comment

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