Open WordPress 'Add New Post' admin page with parameters set via $_GET

All we need is an easy explanation of the problem, so here it is.

I want to launch my web browser from an automation tool I use and open WordPress “Add Nwe Post” page in the admin area with specific title and content (it varies everytime, I generate it dynamically on my local machine).

I know that I can use http://blog.mysite.com/wp-admin/post-new.php?post_title=sometitle

This is fine. However, if I try to set the content of the post, using “content” url parameter, it can be only plain text. If I set HTML, it is escaped automatically. Is there a way to set the HTML content of the post?

Also, I don’t know how I can set page categories via url parameters too?

P.S.: I don’t want to create programatically a new post, but just to have the Add Post page open with prefilled fields.

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

The problem is that $content is a reserved variable in WordPress, you have to use another name. Here, I have used $pre_content:

Open Wordpress 'Add New Post' admin page with parameters set via $_GET

<?php
/**
 * Plugin Name: T5 Editor content by request
 * Description: Default text for post content from GET variable <code>pre_content</code>.
 * Author:      Fuxia Scholz
 * Version:     2012.06.30
 */

/*
 * See wp-admin/includes/post.php function get_default_post_to_edit()
 * There are also the filters 'default_title' and 'default_excerpt'
 */
add_filter( 'default_content', 't5_content_by_request', 10, 2 );

/**
 * Fills the default content for post type 'post' if it is not empty.
 *
 * @param string $content
 * @param object $post
 * @return string
 */
function t5_content_by_request( $content, $post )
{
    if ( ! empty ( $_GET['pre_content'] )
        and current_user_can( 'edit_post', $post->ID )
        and '' === $content
    )
    {
        return $_GET['pre_content'];
    }

    return $content;
}

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply