WordPress.org

Ready to get started?Download WordPress

Forums

preset admin menu links to create postings with specific category, tags, etc.? (4 posts)

  1. codegrunt
    Member
    Posted 2 years ago #

    Howdy. I did some initial searching and could not find any obvious examples so am posting here. If this is a FAQ, my apologies (point me at the docs and I will scurry away).

    What I am wanting to do is to setup some preset menu entries in the admin menu that allow the site editor to create postings with various attributes already chosen for them. An example would be a link "add painting" which loads up edit.php with the category of "paintings" already chosen and the format set to "gallery".

    Adding an admin menu entry that links to a list of articles in a specific category is simple via the theme's functions.php file:

    add_submenu_page( 'edit.php', 'paintings', 'paintings', 'manage_options', 'edit.php?post_type=post&category_name=paintings' );

    However, I am guessing that for creating a link that does the same for editing articles this would require a plugin of some sort or can this be done solely in a theme's "functions.php" file? Ideally what I would do is create some presets that I can then target in the URL like:

    edit.php?posting_type=painting
    edit.php?posting_type=sculpture

    etc.

    If this has to be a plugin, any pointers to an existing plugin that could act as a tutorial for manipulating how edit.php works?

    Cheers

  2. bcworkz
    Member
    Posted 2 years ago #

    It's not too hard to change the admin menus, but you will find it will not always work from a theme, it can only reliably be done from a plugin.

    I think it would be difficult to pass custom parameters to edit.php without a major core hack. But I won't say it's impossible, I don't know for sure. Definitely search for plugins that do something similar.

  3. CircleReader
    Member
    Posted 2 years ago #

    I've been trying to crack this one as well, and have been working from this starting point: http://forums.whirlpool.net.au/archive/1608032. The function defines a $new_post array with a category given in the url, uses wp_insert_post($new_post) to add it to the database & give it a `$post_ID', and then redirects to the editor page for the new post.

    I'm adding the link not to the admin menus, but to the dashboard widget of the <a href="http://wordpress.org/extend/plugins/wp-category-manager/">Category Manager</a> plugin. At the moment my link looks like http://example.com/wp-content/plugins/my-plugin/wpcm-newPost.php?category=8, and the content of that file is a bit simplified from the original, with some echo statements to help me see what's happening:

    function new_post_category() {
    	echo '<div>Hello world! Go add a new one!';
    	$category = $_GET['category']; // get the category from the url
    	echo ' This should be a new post in category ' . $category . '</div>'; // checking to see if we've got it
    	$new_post = array(
    		'post_status' => 'draft', // set post status to draft - we don't want the new post to appear live yet.
    		'post_date' => date('Y-m-d H:i:s'), // set post date to current date.
    		'post_type' => 'post', // set post type to post.
    		'post_category' => array($category) // set category to the category/categories parsed in your previous array
            );
    	print_r($new_post); //checking to see that the $new_post array is there
    	$post_ID = 'post_ID_string'; //putting something in this to echo
    	$post_ID = wp_insert_post( $new_post ); // add the new post to the WordPress database. This should return the ID of the new post, replacing the original value of $post_ID (right?)
    	echo '<div>This should be the new post ID: ' . $post_ID . '</div>';//
    	$new_post_redirect = 'http://'.$_SERVER['SERVER_NAME'].'/wp-admin/post.php?action=edit&post='.$post_ID; // construct url for editing of post
    	echo 'Redirect to ' . $new_post_redirect;
    	// wp_redirect($post_redirect); redirect to edit page for new post.
    	exit;
    }
    
    new_post_category();

    It's pretty straightforward, and it should work. And it does -- except for the wp_insert_post() statement. With that (and the final redirect) commented out the link gives me:

    <blockquote>
    Hello world! Go add a new one! This should be a new post in category 8
    Array ( [post_status] => draft [post_date] => 2012-07-11 01:44:29 [post_type] => post [post_category] => Array ( [0] => 8 ) )
    This should be the new post ID: post_ID_string
    Redirect to http://example.com/wp-admin/post.php?action=edit&post=post_ID_string

    </blockquote>

    The category is there, the post array is there; and the $post_ID is as it was assigned. If I uncomment the `wp_insert_post()' function, though, instead of simply replacing "post_ID_string" with the new ID, I get:

    Hello world! Go add a new one! This should be a new post in category 8
    Array ( [post_status] => draft [post_date] => 2012-07-11 01:51:49 [post_type] => post [post_category] => Array ( [0] => 8 ) )
    Fatal error: Call to undefined function wp_insert_post() in /home/... /wp-content/plugins/wp-category-manager/wpcm-newPost.php on line 16

    It seems that calling the file directly does not give it access to WP core functions (http://wordpress.stackexchange.com/questions/7398/how-to-include-php-files-in-plugins-the-correct-way), and I've been unable to figure out the proper way to call this so those functions are included. Which path to include? Or should this be a template that gets called instead of a direct call to the file? Or...?

  4. bcworkz
    Member
    Posted 2 years ago #

    Correct, plugin files are not normally called directly from a client. You will see that admin files such as post.php, which are called from a client, all have a line similar to:
    require_once('./admin.php');
    which loads the WP admin environment. You probably need to do something similar with a corrected path specification. (for similar front end calls, you would require wp-load.php instead) For proper admin security you should use check_admin_referer().

    I agree that automated creation of a post, then redirecting to the post.php edit action or equivalent seems to be the only reasonable way to pre-populate a post with content.

Topic Closed

This topic has been closed to new replies.

About this Topic