WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Redirect after Publishing Post (3 posts)

  1. Aeriff
    Member
    Posted 7 years ago #

    Hi there, I am developing a plugin to redirect to an external page after publishing my post. I have read about Hooks and Actions and have come up with the following code:

    <?
    
    add_action('post_publish', 'redirect', 0);
    
    function redirect($nothing) {
    	$permalink = get_permalink($_GET["posted"]);
    	$location = "http://www.somesite.com/?url=$permalink";
    	wp_redirect($location);
    }
    
    ?>

    WordPress isn't even detecting the plugin even though it's activated, it's still redirecting to the usual New Post page with the box saying the post has been published. The $_GET["posted"] is for getting the post ID from the post-new.php?posted=ID page (if that will work?). I'd appreciate any ideas on how to get the post ID and then the permalink also by using this plugin, or if someone could point me in the right diretions. Someone even fixing my code so that it will work and reposting it would be greatly appreciated as this is my first plugin.

    Cheers.

  2. golddave
    Member
    Posted 7 years ago #

    Have you found an answer to this question?

  3. golddave
    Member
    Posted 7 years ago #

    I solved this problem! There were a couple of things wrong with your code. Here's the code that worked for me.

    add_action('publish_post','redirect');

    function redirect($post_id) {
    $permalink = get_permalink($post_id);
    $location = "http://www.somesite.com/?url=".$permalink;
    wp_redirect($location);
    }

    Notes:
    1 - The action is publish_post (not post_publish)
    2 - Pass $post_id to the redirect function instead of $nothing
    3 - Run get_permalink on $post_id instead of ($_GET["posted"])
    4 - You concatenated $permalink to the $location improperly. See the difference in the way I did it.

Topic Closed

This topic has been closed to new replies.

About this Topic