Can I turn a WordPress Draft into a published post with a direct link if I know the post ID?
As a WordPress developer, you may often find yourself in a situation where you need to quickly publish or update the status of a post. Whether you're working on a client website or managing your own blog, having the ability to programmatically control the post status can be a valuable tool. In this article, we'll explore how you can use a simple code snippet to convert a WordPress draft post into a published post, or vice versa, using the post ID.
Understanding the Code
Let's take a closer look at the code snippet you provided:
add_action( 'admin_init', 'draft_post_status_221' );
function draft_post_status_221(){
// Get current page , so this action will only fire in post.php page.
global $pagenow;
if ( $pagenow != 'post.php' ){
return;
}
$post_id = false;
$action = false;
// get post id
if ( isset($_GET['post']) && !empty($_GET['post']) ){
$post_id = $_GET['post'];
}
// get action
if ( isset($_GET['action']) && !empty($_GET['action']) ){
$action = $_GET['action'];
// for security we only allow draft action
if ( $action != 'draft' ){
$action = false;
}
}
// if $post_id and $action has data than post will be updated.
if ( !empty($post_id) && !empty($action) ){
$args = array(
'ID' => $post_id,
'post_status' => $action
);
wp_update_post( $args );
}
}
- The code hooks into the
admin_init
action, which means it will only run on the WordPress admin dashboard.
- The
draft_post_status_221()
function is the main logic of the code.
- The function first checks if the current page is the
post.php
page, which is the post edit screen. If not, it returns early to avoid unnecessary processing.
- It then retrieves the
$post_id
and $action
parameters from the URL query string. The $action
parameter is limited to only the 'draft'
value for security reasons.
- If both
$post_id
and $action
are present, the function uses the wp_update_post()
function to update the post status to the specified $action
value.
In the description, you mentioned that this code can be used to convert a draft post into a published post. However, the code as it is currently written can only change the post status to 'draft'
. To change a draft post to a published post, you'll need to modify the code slightly.
Modifying the Code to Publish a Draft Post
Here's the updated code that will allow you to publish a draft post:
add_action( 'admin_init', 'manage_post_status_221' );
function manage_post_status_221(){
// Get current page, so this action will only fire in post.php page.
global $pagenow;
if ( $pagenow != 'post.php' ){
return;
}
$post_id = false;
$action = false;
// Get post ID
if ( isset($_GET['post']) && !empty($_GET['post']) ){
$post_id = $_GET['post'];
}
// Get action
if ( isset($_GET['action']) && !empty($_GET['action']) ){
$action = $_GET['action'];
// Allow 'draft' and 'publish' actions
if ( $action != 'draft' && $action != 'publish' ){
$action = false;
}
}
// If $post_id and $action have data, update the post status
if ( !empty($post_id) && !empty($action) ){
$args = array(
'ID' => $post_id,
'post_status' => $action
);
wp_update_post( $args );
}
}
The main changes are:
- The function name is changed to
manage_post_status_221
to better reflect its purpose.
- The
$action
variable is now allowed to be either 'draft'
or 'publish'
.
- The
wp_update_post()
function is used to update the post status based on the $action
parameter.
Now, to publish a draft post, you can use the following URL structure:
https://your-wordpress-site.com/wp-admin/post.php?post=123&action=publish
Replace 123
with the actual post ID you want to publish.
Similarly, to change a published post to a draft, you can use the following URL structure:
https://your-wordpress-site.com/wp-admin/post.php?post=123&action=draft
This code snippet can be added to your WordPress theme's functions.php
file or a custom plugin.
Benefits of Using This Code
- Efficiency: This code snippet allows you to quickly update the status of a post without having to manually edit the post in the WordPress admin area.
- Automation: You can use this code to create custom workflows or scripts that automatically publish or draft posts based on certain conditions or events.
- Flexibility: The code can be further extended to include additional actions, such as setting the post to a different status (e.g., private, pending, etc.).
- Security: The code includes a security check to ensure that only the
'draft'
and 'publish'
actions are allowed, preventing potential misuse.
Potential Use Cases
Here are a few examples of how you can use this code:
- Scheduled Post Publishing: You can create a scheduled task (e.g., a cron job) that checks for posts in the draft status and publishes them at a specific time.
- Bulk Post Management: You can create a custom admin page or tool that allows you to easily manage the status of multiple posts at once.
- Integrations: You can use this code as part of a larger integration, such as a plugin that synchronizes content between different platforms or systems.
- Troubleshooting: If you ever encounter a situation where a post is stuck in the draft status and you need to quickly publish it, this code can be a useful tool.
Remember, while this code snippet can be a helpful tool, it's important to use it responsibly and with caution, as it can potentially impact the visibility and publication status of your WordPress content.
If you're looking for a more comprehensive solution to manage your website's content and conversion rates, consider checking out Flowpoint.ai. Flowpoint's advanced analytics and AI-powered recommendations can help you identify technical issues, optimize user experience, and boost your website's overall performance
Get a Free AI Website Audit
Automatically identify UX and content issues affecting your conversion rates with Flowpoint's comprehensive AI-driven website audit.