Support » Plugins » using get_permalinks() in a plugin

  • greetings collective mind,

    I’m writing a simple plugin to filter an html comment into a form, and would like it to have the form action be the page it’s on. I thought I would be able to use get_permalinks for the action uri, but I’m getting an error.

    I’m testing this on a page (not a post). It’s fairly basic, with an add_filter. Outside of the add filter, I build the form, which uses get_permalink(), or I process the received post data.

    The error returned is “Fatal error: Call to undefined function: get_userdata() in /var/www/html/info/wp-includes/template-functions-links.php on line 63”

    Am I missing something basic here? Is there a way to use it in a plugin?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator Samuel Wood (Otto)

    (@otto42) Admin

    When, exactly, is the get_permalink() call *executed*? If you’re actually executing it too early, then all the functions may not have loaded yet.

    Remember that plugins execute very early in the process. Generally, you don’t actually execute any code in plugins, you just define functions that get executed at various other points during the page creation process, using hooks or direct calls from the theme.

    In particular, get_permalink() makes a reference to get_userdata(), which is in pluggable-functions.php. Pluggable functions doesn’t load until *after* plugins load, so at the very minimum, you’d need to delay that call until the ‘plugins_loaded’ action hook.

    The other problem is that you’re trying to call get_permalink for the current page. This relies on the database having been initialized and the relevant posts loaded, which means that you should probably wait at least until the ‘init’ action hook before attempting to use get_permalink in any significant way.

    So wrap the code you want to run into a function, then add_action(‘init’,’your_function’); It’ll run a bit later and might eliminate your issues. If you still have trouble with it, follow it through and find a later action hook that suits your needs. Ideally, you want to delay execution of any code as long as possible, until the moment you need the output from that code. But sometimes it’s helpful to generate it in advance and use it later. Not often though.

    Thanks for that fairly detailed response Otto. I’m not really familiar enough with the order things load. I expect you would say something like this though. I’ve poked through another plugin that does something similar, and saw them do more or less what I’m doing. But as the plugin is considerably more complicated, I’ve obviously missed something key they’ve also done.

    I’ve tried the call inside a function that add_filter calls. I still get an undefined function error. I’ll read through what you said tomorrow and give this another go then.

    Thanks again.

    Moderator Samuel Wood (Otto)

    (@otto42) Admin

    Start at wp-blog-header.php and follow the includes and requires. Along the way, try to generalize the bits into logical sections. It helps a lot when you’re trying to figure out how to write plugins that change things.

    well, I have one of those ‘not sure why it’s now working’ situations. this morning, I went to my code, and cleaned it up. I basically cleared out the junk, and properly formatted it with proper indents and such so I can make sense of it. I had a lot of test items commented out and such, so I removed those, otherwise, I didn’t think I changed the code. However, it now works. It seems fine to have the get_permalink inside the add_filter hook function.

    I can only assume I had a typo before that I missed or something. I don’t have the old version anymore to check against. I appreciate the help Otto, and for the benefit of others, I’ll post the ‘proof of concept’ code here (it’s short).

    function cptc_registration_form($content)

    if(isset($_POST['submitted'])) {
    $form = "I think something was submitted";
    $permalink = get_permalink();

    $form = '
    <form name="example" action="' . get_permalink() . '" method="post">
    Text: <input type="text" name="text" />
    <input type="hidden" name="submitted" />
    <input type="submit" value="Go!" />

    return str_replace('<!--cptc registration form-->', $form, $content);


    add_filter('the_content', 'cptc_registration_form');

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘using get_permalinks() in a plugin’ is closed to new replies.