Wordpress - custom post status is not showing - wordpress

I've created new post status. Here's code from my custom plugin:
add_action('init', 'new_post_status_add');
function new_post_status_add () {
register_post_status('refused', array(
'label' => _x('Refused', 'post'),
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop('Refused <span class="count">(%s)</span>', 'Refused <span class="count">(%s)</span>'),
));
}
But it's not working - not visible in edit form and quick edit form:
What should I do to make status available?

Sorry for delayed reply. I hope my comment would help others.
Custom Post Status is an unsolved issue that has been there for such a long time, or at least since 8yrs ago on Wordpress website here.
Wordpress noted on codex document for function register_post_status():
This function does NOT add the registered post status to the admin panel. This functionality is pending future development. Please refer
to Trac Ticket #12706. Consider the action hook
post_submitbox_misc_actions for adding this parameter.
There is a work around that I could use for one of my projects. I mixed solutions that described on this url and this url.
The summary is that Wordpress doesn't show custom post status automatically. It needs to be manipulated by using your theme's function.php.
First step
Create custom post status, same as you mentioned. just, don't forget that "This function should not be called before the 'init' action."
‌
function j_custom_post_status() {
$args = array(
'label' => _x( 'Refused', 'post'),
'label_count' => _n_noop( 'Refused <span class="count">(%s)</span>', 'Refused (%s)', 'post' ),
'public' => false,
'internal' => true,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'exclude_from_search' => false,
);
register_post_status( 'j_refused', $args );
add_action( 'init', 'j_custom_post_status', 0 );
‌
I use WP Generator to get the code snippet above.
Second step
Tweak Admin Panel to show the custom status in Quick Edit Menu (QE) on posts list page edit.php. We need JQuery's help to do so and we need to add JQuery code to Admin Panel footer.
‌
function j_append_status_list(){
//array of all custom status that you created
$arr_customstatus = array(
'refused',
'other_custom_status',
);
echo '<script>';
//flush
$tmp = '';
//generate string
foreach ($arr_customstatus as $pkey ) {
$tmp.= '<option value="'. $pkey .'">'. $pkey .'</option>';
}
echo "jQuery(document).ready( function() { jQuery( 'select[name=\"_status\"]' ).append( ". $tmp ." );
});";
echo '</script>';
}
add_action('admin_footer-edit.php','j_append_status_list');
‌
Note: The function above doesn't automatically select the status in QE in case the post's status has been modified before. To add the selected="true" to the option, you need to use the global $post and check the current status, same as the the method used in the Third step below.
Third step
Now, we need to add custom post status meta to post edit page too.
function j_custom_status_metabox(){
global $post;
$custom = get_post_custom( $post->ID );
$status = $custom["_status"][0];
// Array of custom status messages
$arr_customstatus = array(
'refused',
'other_custom_status',
);
//Flush
$tmp = '';
foreach( $jstats as $pkey ) {
if( $status == $pkey ){
$tmp.= '<option value="'.$pkey.'" selected="true">'. $pkey .'</option>';
} else {
$tmp.= '<option value="'. $pkey .'">'. $pkey .'</option>';
}
}
echo '<script>';
echo "jQuery(document).ready( function() { jQuery( 'select[name=\"_status\"]' ).append( ". $tmp ." );
});";
echo '</script>';
}
add_action('admin_footer-post.php','j_custom_status_metabox');
add_action('admin_footer-post-new.php','j_custom_status_metabox');
‌
Step four
In case you like to show a message in posts lists based on custom post status, you can add this function too. However, this is optional.
function j_display_status_label( $statuses ) {
global $post;
//Display label on all posts list but not on the custom status list
if( get_query_var( 'post_status' ) != 'refused' ){
if( $post->post_status == 'refused' ){
return array('Refused');
}
}
return $statuses;
}
add_filter( 'display_post_states', 'j_display_status_label' );
‌‌ ‌
Note: The function above doesn't include all future custom status. You need to add foreach loop in case you need.

Try to use this code,
// Register Custom Status
function custom_post_status() {
$args = array(
'label' => _x( 'Refused', 'Status General Name', 'text_domain' ),
'label_count' => _n_noop( 'Refused (%s)', ' (%s)', 'text_domain' ),
'public' => true,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'exclude_from_search' => false,
);
register_post_status( 'refused', $args );
}
add_action( 'init', 'custom_post_status', 0 );
I hope this will work for you.

Try adding PHP_INT_MAX to your j_custom_post_status action
add_action( 'init', 'j_custom_post_status', PHP_INT_MAX );
‌

Related

how to show a backorder status in woocommerce orders listing

We have a lot of backorders in WooCommerce and we would like to be able to sort all the backorders in the orders table.
Currently, we are filtering the orders using these status: Awaiting shipment, Refunded, Cancelled, Completed, Processing
Please refer to the screenshot
I've searched online and I only found a plugin that create a separate menu link lisitng backorders only.
Is there a filter I could add in functions.php that would do the job ? I don't want to install a plugin just for that.
Right Now i've only been able to add a custom backorder status using this code below, the second step would be to trigger the status when there is a backordered product.
/**
* Register new status
* Tutorial: http://www.sellwithwp.com/woocommerce-custom-order-status-2/
**/
function register_backorders_order_status() {
register_post_status( 'wc-backorders', array(
'label' => 'Backorders',
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( 'backorders <span class="count">(%s)</span>', 'Awaiting shipment <span class="count">(%s)</span>' )
) );
}
add_action( 'init', 'register_backorders_order_status' );
// Add to list of WC Order statuses
function add_backorders_to_order_statuses( $order_statuses ) {
$new_order_statuses = array();
// add new order status after processing
foreach ( $order_statuses as $key => $status ) {
$new_order_statuses[ $key ] = $status;
if ( 'wc-processing' === $key ) {
$new_order_statuses['wc-backorders'] = 'Backorders';
}
}
return $new_order_statuses;
}
add_filter( 'wc_order_statuses', 'add_backorders_to_order_statuses' );

Woocommerce - Sending custom email from custom order status

I'm new to Wordpress and I'm trying to figure out how to send an email when my order status is changed to a specific custom order status.
Here is my code:
function register_awaiting_shipment_order_status() {
register_post_status( 'wc-awaiting-shipment', array(
'label' => 'Shipped',
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( 'Awaiting shipment <span class="count">(%s)</span>', 'Awaiting shipment <span class="count">(%s) </span>' )
) );
}
add_action( 'init', 'register_awaiting_shipment_order_status' );
// Add to list of WC Order statuses
function add_awaiting_shipment_to_order_statuses( $order_statuses) {
$new_order_statuses = array();
// add new order status after processing
foreach ( $order_statuses as $key => $status ) {
$new_order_statuses[ $key ] = $status;
if ( 'wc-processing' === $key ) {
$new_order_statuses['wc-awaiting-shipment'] = 'Shipped';
// WC()->mailer()->emails['wc-awaiting-shipment']->trigger($order_id);
}
}
return $new_order_statuses;
}
add_filter( 'wc_order_statuses', 'add_awaiting_shipment_to_order_statuses' );
How would I send an email to the customer when their order status is changed to this custom order status ('shipped')?
Thanks in advance
You can use this plugin for your solution.
WooCommerce Order Status Change Notifier
Or
You can go to plugin settings => Email tab and install (enable) notifications that you want.
Hope this will help you

Update wordpress post from front-end (using acf)

I am making front end admin for users to add/edit their posts. I already made post add form and it works, but edit form doesnt work.
functions.php
function add_new_post( $post_id )
{
if( $post_id == 'new' ) {
// Create a new post
$post = array(
'post_title' => $_POST["fields"]['field_52c810cb44c7a'],
'post_category' => array(4),
'post_status' => 'draft',
'post_type' => 'post'
);
// insert the post
$post_id = wp_insert_post( $post );
return $post_id;
}
else {
return $post_id;
}
}add_filter('acf/pre_save_post' , 'add_new_post' );
index.php
<div id="updateform-<?php the_ID(); ?>" class="collapse">
<?php
echo get_the_ID();
$args = array(
'post_id' => get_the_ID(), // post id to get field groups from and save data to
'field_groups' => array(31), // this will find the field groups for this post (post ID's of the acf post objects)
'form' => true, // set this to false to prevent the <form> tag from being created
'form_attributes' => array( // attributes will be added to the form element
'id' => 'post',
'class' => '',
'action' => get_permalink( get_the_ID() ),
'method' => 'post',
),
'return' => add_query_arg( 'updated', 'true', get_permalink() ), // return url
'html_before_fields' => '', // html inside form before fields
'html_after_fields' => '', // html inside form after fields
'submit_value' => 'Update', // value for submit field
'updated_message' => 'Post updated.', // default updated message. Can be false to show no message
);
acf_form( $args );
?>
</div>
For saving posts you should use save_post not pre_save_post.
Basically pre_save_post is used for new posts.
Use this below add_filter('save_post' , 'add_new_post');
I created plugins for that:
Forms actions
https://wordpress.org/plugins/forms-actions/
Add actions to yours ACF forms.
ACF Frontend display
https://wordpress.org/plugins/acf-frontend-display/
Display your ACF form on frontend
If by some chance you happen to be using Elementor Page Builder, you can do this by installing Advanced Widgets for Elementor. It comes with an ACF Form widget that you can just drag & drop anywhere into your site and configure your form through the UI (no coding required).

Wordpress: get_delete_post_link not working for custom role

I have a custom role in functions.php:
add_role('test_pilot', 'Test Pilot', array(
'read' => true,
'edit_posts' => true,
'delete_posts' => true,
));
// Give the custom role a new level
$test_pilot = get_role('test_pilot');
$test_pilot->add_cap('level_3');
...and on the front-end I'm trying to echo the delete post link:
<?php echo get_delete_post_link( get_the_ID() ); ?>
The problem is the link isn't actually being displayed when logged in as a user with the test pilot role.
If I am logged in as an administrator the link does display.
What am I doing wrong?
Try to replacing with below code:
function init_roles() {
global $wp_roles;
if (class_exists('WP_Roles'))
if ( ! isset( $wp_roles ) )
$wp_roles = new WP_Roles();
if (is_object($wp_roles)) :
$wp_roles->add_cap( 'editor');
endif;
$wp_roles->add_role( 'test_pilot', 'Test Pilot', array(
'read' => true,
'edit_posts' => true,
'delete_posts' => true
));
}
add_action('init', 'init_roles');

How to change Admin Wordpress post list template for CPT?

I'm trying to change the back end post listing template for a custom post I added to wordpress. For clarity, the image below shows what I mean by "Post listing template":
The regular post listing template shows "Title", "Author", "Categories", "Tags", "Languages", and "Date" fields. However, my custom post has extra functionality and other custom fields I'd like to add to this template, but I can't find the method.
Well what would you like to add? Most likely you'll have to go digging in Wordpress code to change that. I've never seen a plugin for this kind of functionality. What more do you want added?
The code you would need to look into is "/wp-admin/edit.php". It's quite a long and reasonably complex file. So good luck.
Found the answer!
The "manage__posts_columns" filter can be used for modifying the displayed columns.
The "manage_posts_custom_column" action can be used for modifying the content of these columns.
Code Example for adding custom columns:
function change_columns( $cols ) {
$cols = array(
'cb' => '<input type="checkbox" />',
'title' => __( 'Title', 'trans' ),
'vin' => __( 'Vin #', 'trans' ),
'make' => __( 'Make', 'trans' ),
'model' => __( 'Model', 'trans' ),
'year' => __( 'Year', 'trans' ),
'color' => __( 'Color', 'trans' ),
'thumbnail' => __( 'Image', 'trans' ),
);
return $cols;
}
add_filter( "manage_tek_car_post_posts_columns", "change_columns" );
Code Example for adding content to these columns:
function custom_columns( $column, $post_id ) {
switch ( $column ) {
case "vin":
$colvin = get_post_meta( $post_id, 'vin', true);
echo $colvin;
break;
case "make":
$colmake = get_post_meta( $post_id, 'carmake', true);
echo $colmake;
break;
case "model":
$colmodel = get_post_meta( $post_id, 'carmodel', true);
echo $colmodel;
break;
case "year":
$colyear = get_post_meta( $post_id, 'caryear', true);
echo $colyear;
break;
case "color":
$colcolor = get_post_meta( $post_id, 'excolor', true);
echo $colcolor;
break;
}
}
add_action( "manage_posts_custom_column", "custom_columns", 10, 2 );
Source: http://yoast.com/custom-post-type-snippets/
I just added something like this under wp-admin/includes/class-wp-list-table.php
<table class="wp-list-table <?php echo implode( ' ', $this->get_table_classes() ); ?>" cellspacing="0">
<thead>
<tr>
SWapnesh
</tr>
and its all getting correctly on wp-admin/edit.php page (code is around line num 775 in my wordpress)

Resources