How can i change published text under date column in wordpress? - php

I want to change text of published under date column of post list in wordpress.
see below image

You can try using following way.
<?php
function my_custom_columns( $columns ) {
unset( $columns['date'] );
$columns['mydate'] = 'My Custom Date';
return $columns;
}
function my_format_column( $column_name , $post_id ) {
if($column_name == 'mydate'){
echo get_the_time( 'l, F j, Y', $post_id )."<br>".get_post_status( $post_id );
}
}
function my_column_register_sortable( $columns ) {
$columns['mydate'] = 'mydate';
return $columns;
}
function my_column_orderby( $vars ) {
if ( isset( $vars['orderby'] ) && 'mydate' == $vars['orderby'] ) {
$vars = array_merge( $vars, array(
'orderby' => 'date'
) );
}
return $vars;
}
function my_column_init() {
add_filter( 'manage_posts_columns' , 'my_custom_columns' );
add_action( 'manage_posts_custom_column' , 'my_format_column' , 10 , 2 );
add_filter( 'manage_edit-post_sortable_columns', 'my_column_register_sortable' );
add_filter( 'request', 'my_column_orderby' );
}
add_action( 'admin_init' , 'my_column_init' );
?>

Related

Sortable columns in Wordpress for a Custom Post Type

I'm working on the admin side for a Wordpress site. I have a CPT that I've added a column into and would like to sort by that column type. Based off what I've read I'm using the correct functions, but after the filter of 'manage_edit-leadership_sortable_columns' there are no changes to the admin UI. This a CPT and the hierarchy is set to 'True.' I'd read that that may affect it but couldn't find a fix. The column also appears with a red circle and '0' next to it, that I don't know where it came from. The eventual goal is to set the display page on the site to the menu order and give the client the option to sort either by Title or Last Name. Thanks!
add_filter( 'manage_leadership_posts_columns', 'smashing_filter_posts_columns' );
function smashing_filter_posts_columns( $columns ) {
$name = array();
$name['last_name'] = __( 'Last Name', 'smashing', true );
array_splice($columns, 2, 0, $name);
return $columns;
}
function smashing_leadership_column( $column, $post_id ) {
// Name column
if ( 'last_name' == $column ) {
$lastName = get_field('last_name', $post_id );
echo $lastName;
}
}
add_action( 'manage_leadership_posts_custom_column', 'smashing_leadership_column', 10, 2);
function smashing_leadership_sortable_columns( $columns ) {
$columns['last_name'] = 'last_name';
return $columns;
}
add_filter( 'manage_edit-leadership_sortable_columns', 'smashing_leadership_sortable_columns');
add_action( 'pre_get_posts', 'smashing_posts_orderby' );
function smashing_posts_orderby( $query ) {
if( ! is_admin() || ! $query->is_main_query() ) {
return;
}
if ( 'last_name' === $query->get( 'orderby') ) {
$query->set( 'orderby', 'meta_value' );
$query->set( 'meta_key', 'last_name' );
}
}
Admin Screenshot
I was able to get this to work with the following code in place of the other sortable filters.
add_filter( 'manage_edit-leadership_sortable_columns', 'my_sortable_leadership_columns' );
function my_sortable_leadership_columns( $columns ) {
$columns['firstName'] = 'first';
$columns['lastName'] = 'last';
return $columns;
}
add_action( 'pre_get_posts', 'my_leadership_orderby' );
function my_leadership_orderby( $query ) {
if( ! is_admin() )
return;
$orderby = $query->get( 'orderby');
if( 'first' == $orderby ) {
$query->set('meta_key','first_name');
$query->set('orderby','meta_value');
}
if( 'last' == $orderby ) {
$query->set('meta_key','last_name');
$query->set('orderby','meta_value');
}
}

WordPress: Order admin user column by number

I want to order an admin column in the user overview list by a custom ID.
To do so, I added a function to sort the column but it doesn't work as expected.
It kind of sorts the fields but in a strange way. I guess there is an other sorting in place or the function has an error?!
This is the code for adding the ID to the user:
add_action( 'edit_user_profile', 'wcv_store_custom_vendor_id', 0 );
//add_action( 'wcv_admin_after_store_address', 'wcv_store_custom_vendor_id' );
function wcv_store_custom_vendor_id( $user ) {
?>
<table class="form-table">
<tbody>
<tr>
<th><label for="_wcv_custom_vendor_id"><?php _e( 'Vendor ID', 'wcvendors-pro' ); ?></label></th>
<td><input type="text" name="_wcv_custom_vendor_id" id="_wcv_custom_vendor_id" value="<?php echo get_user_meta( $user->ID, '_wcv_custom_vendor_id', true ); ?>" class="regular-text"></td>
</tr>
</tbody>
</table>
<?php
}
// Save the details on the back end when updating the user
add_action( 'wcvendors_update_admin_user', 'save_wcv_custom_vendor_id' );
function save_wcv_custom_vendor_id( $user_id ){
if ( isset( $_POST['_wcv_custom_vendor_id'] ) ) {
update_user_meta( $user_id, '_wcv_custom_vendor_id', $_POST['_wcv_custom_vendor_id'] );
}
Here's what I have so far:
// Add column to admin
add_action('manage_users_columns', 'add_custom_vendor_id_column', 10, 1 );
function add_custom_vendor_id_column( $columns ) {
$columns['wcv_custom_vendor_id'] = __('Vendor-ID');
return $columns;
}
// fetching the status, thanks to LoicTheAztec
add_filter('manage_users_custom_column', 'add_data_to_vendor_id_column', 10, 3);
function add_data_to_vendor_id_column( $value, $column_name, $user_id ) {
if ( 'wcv_custom_vendor_id' == $column_name ) {
if( get_user_meta( $user_id, '_wcv_custom_vendor_id', true ) != '' ) {
$value = '<span style="color:green;font-weight:bold;">'.get_user_meta( $user_id, '_wcv_custom_vendor_id', true ).'</span>';
} else {
$value = '<span class="" style="color:red;font-weight:bold;">No ID!</span>';
}
}
return $value;
}
// make admin colum sortable
function fc_my_sortable_cake_column( $columns ) {
$columns['wcv_custom_vendor_id'] = 'Vendor-ID';
return $columns;
}
add_filter( 'manage_users_sortable_columns', 'fc_my_sortable_cake_column' );
// second try to sort by number, no effect?!
function sort_datanowa_column( $vars ) {
if ( isset( $vars['orderby'] ) && 'Vendor-ID' == $vars['orderby'] ) {
$vars = array_merge( $vars, array(
'meta_key' => '_wcv_custom_vendor_id', // Find correct meta field key
'orderby' => 'meta_value_num'
) );
}
return $vars;
}
add_filter( 'request', 'sort_datanowa_column' );
I checked a lot of snippets and the last function should sort by number. But I think it has no effect at all.
Is there anythin I miss?
Use pre_get_users opposite request
For empty values ​​for the meta_key we use the following: update_user_meta( $user_id, '_wcv_custom_vendor_id', 0 );
So you get:
// Add column to admin
function add_custom_vendor_id_column( $columns ) {
$columns['wcv_custom_vendor_id'] = __( 'Vendor-ID', 'woocommerce');
return $columns;
}
add_action( 'manage_users_columns', 'add_custom_vendor_id_column', 10, 1 );
// fetching the status, thanks to LoicTheAztec
function add_data_to_vendor_id_column( $value, $column_name, $user_id ) {
if ( $column_name == 'wcv_custom_vendor_id' ) {
$vendor_id = (int) get_user_meta( $user_id, '_wcv_custom_vendor_id', true );
// If empty
if ( empty ($vendor_id ) ) {
update_user_meta( $user_id, '_wcv_custom_vendor_id', 0 );
}
if ( $vendor_id && $vendor_id != 0 ) {
$value .= '<span style="color:green;font-weight:bold;">' . $vendor_id . '</span>';
} else {
$value = '<span class="" style="color:red;font-weight:bold;">No ID!</span>';
}
}
return $value;
}
add_filter( 'manage_users_custom_column', 'add_data_to_vendor_id_column', 10, 3);
// make admin colum sortable
function fc_my_sortable_cake_column( $columns ) {
$columns['wcv_custom_vendor_id'] = __( 'Vendor-ID', 'woocommerce');
return $columns;
}
add_filter( 'manage_users_sortable_columns', 'fc_my_sortable_cake_column', 10, 1 );
// orderby
function action_pre_get_users( $query ) {
if ( !is_admin() )
return;
$orderby = $query->get('orderby');
if ( $orderby == 'Vendor-ID' ) {
$query->set( 'orderby' , 'meta_value_num' );
$query->set( 'meta_key', '_wcv_custom_vendor_id' );
}
}
add_action( 'pre_get_users', 'action_pre_get_users', 10, 1 );

How to post a value to custom column in admin order panel via Woocommerce API?

In Woocommerce, I have created a custom column to admin orders list page.
<?php
add_filter( 'manage_edit-shop_order_columns', 'MY_COLUMNS_FUNCTION' );
function MY_COLUMNS_FUNCTION( $columns ) {
$new_columns = ( is_array( $columns ) ) ? $columns : array();
unset( $new_columns[ 'order_actions' ] );
$new_columns['MY_COLUMN_ID_1'] = 'MY_COLUMN_1_TITLE';
$new_columns[ 'order_actions' ] = $columns[ 'order_actions' ];
return $new_columns;
}
Then, custom field for this column
add_action( 'manage_shop_order_posts_custom_column', 'MY_COLUMNS_VALUES_FUNCTION', 2 );
function MY_COLUMNS_VALUES_FUNCTION( $column ) {
global $post;
$data = get_post_meta( $post->ID );
if ( $column == 'MY_COLUMN_ID_1' ) {
echo ( isset( $data[ 'MY_COLUMN_1_POST_META_ID' ] ) ? $data[ 'MY_COLUMN_1_POST_META_ID' ] : '' );
}
}
But I don't know how to post a string to the specific custom field via WooCommerce API?
I would tweak Andrew's answer just a little to start using the order object more and rely less on post meta. I'm pretty sure WooCommerce will move at least it's core meta into a custom table eventually.
function wc_add_custom_order_column_content( $column ) {
global $post, $the_order;
if ( empty( $the_order ) || $the_order->get_id() !== $post->ID ) {
$the_order = wc_get_order( $post->ID );
}
// Only continue if we have an order.
if ( empty( $the_order ) ) {
return;
}
if ( 'custom_column' === $column ) {
// Use order class getters to retrieve what you need.
echo $the_order->get_formatted_order_total();
// Or, if it's not a core field, it may be in meta.
// echo $the_order->get_meta('_some_meta_key');
}
}
add_action( 'manage_shop_order_posts_custom_column', 'wc_add_custom_order_column_content' );
Here's an example of how to add a custom column in the admin order screen. It will add your column after the Actions column and populate it with your custom field data. You'll need to replace my "custom_column" values with your own field names.
function wc_add_custom_order_column( $columns ) {
$new_columns = array();
foreach ( $columns as $column_name => $column_info ) {
$new_columns[ $column_name ] = $column_info;
if ( 'order_actions' === $column_name ) {
$new_columns['custom_column'] = __( 'Custom Column', 'my-textdomain' );
}
}
return $new_columns;
}
add_filter( 'manage_edit-shop_order_columns', 'wc_add_custom_order_column', 20 );
function wc_add_custom_order_column_content( $column ) {
global $post;
if ( 'custom_column' === $column ) {
$data = get_post_meta( $post->ID );
if( isset( $data['custom_column'] ) )
echo $data['custom_column'];
}
}
add_action( 'manage_shop_order_posts_custom_column', 'wc_add_custom_order_column_content' );

Wordpress sortable column by IP address

I have been able to add a sortable column to my Wordpress userpage that displays the users last know IP address.
The problem I seem to be having is getting the column to sort by the IP's numbers and not the usernames alphabetically.
I feel im missing something simple and could use another set of eyes.
<?php
class Register_IP_Multisite {
public function __construct() {
add_action( 'init', array( &$this, 'init' ) );
}
public function init() {
add_action( 'user_register', array( $this,'log_ip') );
add_action( 'edit_user_profile', array( $this,'edit_user_profile') );
add_action( 'manage_users_custom_column', array( $this,'columns'), 10, 3);
add_filter( 'manage_users_sortable_columns', array( $this ,'users_sortable_columns_wsp') );
add_filter( 'request', array( $this ,'users_orderby_column_wsp') );
add_filter( 'plugin_row_meta', array( $this ,'donate_link'), 10, 2 );
if ( is_multisite() ) {
add_filter('wpmu_users_columns', array( $this ,'column_header_signup_ip'));
} else {
add_filter('manage_users_columns', array( $this ,'column_header_signup_ip'));
}
}
public function log_ip($user_id){
$ip = $_SERVER['REMOTE_ADDR']; //Get the IP of the person registering
update_user_meta($user_id, 'signup_ip', $ip); //Add user metadata to the usermeta table
}
public function edit_user_profile() {
$user_id = (int) $_GET['user_id'];
?>
<h3><?php _e('Signup IP Address', 'register-ip-mutisite'); ?></h3>
<p style="text-indent:15px;"><?php
$ip_address = get_user_meta($user_id, 'signup_ip', true);
echo $ip_address;
?></p>
<?php
}
public function column_header_signup_ip($column_headers) {
$column_headers['signup_ip'] = __('IP Address', 'register-ip-multisite');
return $column_headers;
}
public function users_sortable_columns_wsp($column_headers) {
$customwsp = array(
// meta column id => sortby value used in query
'signup_ip' => 'user_ip_address',
);
return wp_parse_args($customwsp, $column_headers);
}
public function users_orderby_column_wsp( $varswsp ) {
if ( isset( $varswsp['orderby'] ) && 'signup_ip' == $varswsp['orderby'] ) {
$varswsp = array_merge( $varswsp, array(
'meta_key' => 'signup_ip',
'orderby' => 'meta_value'
) );
}
return $varswsp;
}
public function columns($value, $column_name, $user_id) {
global $modewsp;
$modewsp = empty( $_REQUEST['mode'] ) ? 'list' : $_REQUEST['mode'];
if ( $column_name == 'signup_ip' ) {
$ip = get_user_meta($user_id, 'signup_ip', true);
if ($ip != ""){
$theip = $ip;
if ( has_filter('ripm_show_ip') ) {
$theip = apply_filters('ripm_show_ip', $theip);
}
return $theip;
} else {
$theip = '<em>'.__('None Recorded', 'register-ip-multisite').'</em>';
return $theip;
}
}
$user_ip_address = strtotime(get_date_from_gmt($user->signup_ip));
return $value;
}
}
new Register_IP_Multisite();

get the modified date of post meta box

i'm searching for a way to display the modified date of a custom field / meta box. until now, i only know how to echo
the_modified_date('l, j.n.Y');
but this one only works with the entire post.
i'am using a meta_box for additional pdf files uploading, code:
<?php class PDF_Metabox_id1 {
function __construct() {
add_action( 'post_mime_types', array( $this, 'pdf_mime_type_id1' ) );
add_action( 'add_meta_boxes', array( $this, 'admin_scripts_id1' ), 5 );
add_action( 'add_meta_boxes', array( $this, 'metabox_add_id1' ) );
add_action( 'save_post', array( $this, 'pdf_save_postdata_id1') );
add_action( 'wp_ajax_refresh_pdf', array( $this, 'refresh_pdf_id1' ) ); }
function pdf_mime_type_id1() {
$post_mime_types['application/pdf'] = array( __( 'PDFs' ), __( 'Manage PDFs' ), _n_noop( 'PDF <span class="count">(%s)</span>', 'PDFs <span class="count">(%s)</span>' ) );
return $post_mime_types;
}
function admin_scripts_id1() {
wp_register_script( 'pdf_metabox_js', get_stylesheet_directory_uri() . '/js/pdf_metabox.js' );
}
function metabox_add_id1() {
$post_types = array( 'myposttype','anotherposttype' );
$context = 'normal'; $priority = 'low';
foreach( $post_types as $post_type ) {
add_meta_box( 'pdf_metabox_id1', __( 'PDF Box 1', 'pdf_metabox_id1' ), array( $this, 'pdf_metabox_id1' ), $post_type, $context, $priority );
wp_enqueue_media();
wp_enqueue_script( 'pdf_metabox_js' );
}
}
function pdf_metabox_id1( $post ) {
$original_post = $post; echo $this->pdf_metabox_html_id1( $post->ID ); $post = $original_post;
}
function pdf_item_id1( $id ) {
if(!$id) return; $pdf_url = esc_url_raw(wp_get_attachment_url($id)); $pdf = $pdf_url; return $pdf;
}
function pdf_metabox_html_id1( $post_id ) {
$current_value = ''; $post_meta = get_post_custom($post_id);
if( isset($post_meta['pdf_id_id1'][0] ) ) $current_value = $post_meta['pdf_id_id1'][0];
$return = ''; wp_nonce_field( plugin_basename( __FILE__ ), 'pdf_noncename' );
$return .= '<p>';
$return .= '<a title="'.__( 'PDF', 'pdf_metabox_id1' ).'" class="button button-primary insert-pdf-button" id="insert_pdf_button_id1" href="#" style="float:left">'.__( 'Upload / editiere PDF', 'pdf_metabox_id1' ).'</a><span id="pdf_spinner_id1" class="spinner" style="float:left"></span></p>';
$return .= '<div style="clear:both"></div>';
$return .= '<input type="hidden" name="pdf_id_id1" id="pdf_id_id1" value="'.$current_value.'">';
$return .= '<div style="clear:both"></div>';
$return .= '<div id="pdf_wrapper_id1">';
$pdf = $this->pdf_item_id1( $current_value ); if( empty( $pdf ) ) {
$return .= '<p>Diesem Feld ist kein PDF hinterlegt.</p>'; } else {
$return .= '<br />URL des PDF:<br /> ';
$return .= $pdf; }
$return .= '</div>';
return $return;
}
function pdf_save_postdata_id1($post_id){
if ( isset($_POST['post_type']) && 'post' == $_POST['post_type'] ) {
if ( !current_user_can( 'edit_post', $post_id ) ) return; } if ( !isset( $_POST['pdf_noncename'] ) || ! wp_verify_nonce( $_POST['pdf_noncename'], plugin_basename( __FILE__ ) ) ) return;
if(isset($_POST['pdf_id_id1']) ): update_post_meta($post_id, 'pdf_id_id1', sanitize_text_field( $_POST['pdf_id_id1'] ) );
else: if (isset($post_id)) {
delete_post_meta($post_id, 'pdf_id_id1'); }
endif;
}
function refresh_pdf_id1() {
if(isset($_POST['id'])){
$item = $_POST['id'];
if($item != '' && $item !=0){
$pdf = $this->pdf_item_id1( $item );
$ret = array();
if( !empty( $pdf ) ) {$ret['success'] = true;
$ret['pdf'] = $pdf; } else {
$ret['success'] = false; } } else {
$ret['success'] = true; $ret['pdf'] = ''; } } else {
$ret['success'] = false; } echo json_encode( $ret ); die();
}
}
$PDF_Metabox_id1 = new PDF_Metabox_id1();
in my theme i'm using:
$post_meta_id1 = get_post_custom(get_the_ID());
$pdf_id_id1 = $post_meta_id1['pdf_id_id1'][0];
$pdf_url_id1 = wp_get_attachment_url($pdf_id_id1);
...and now i want to display the modified date of this field. any ideas?
As per the comment
how do i save the modified date of this custom field and how do i get it?
You may need to check every custom field in loop one by one if any of custom fields doesnot matched with the current post data then save a new custom field to post_meta. then retrieve in your template/page.
Just an idea:
<?php
function attributes_save_postdata( $post_id ) {
$postcustom = get_post_custom( $post_id );
$is_modified = false;
foreach ( $postcustom as $key => $val ) {
var_dump( $val );
// check your post custom values and
$getpostmeta = get_post_meta( $post_id, 'your_meta_key', true );
// if database value ($getpostmeta) not equal to current post value($_POST['your_meta_key'])
if ( $getpostmeta != $_POST['your_meta_key'] ) {
$is_modified = true;
// if found any custom field updated set $modified = true
}
}
// if found any custom field updated add or update new date and time
if ( $is_modified ) {
$date = date( 'Y-m-d h:i:s' ); // example : 2016-02-02 12:00:00 current date and time
update_post_meta( $post_id, '_modifieddate', $date );
}
}
add_action( 'save_post', 'attributes_save_postdata' );
Then in your theme. get modified date.
$post_id = get_the_ID();
$getpostmeta = get_post_meta( $post_id, 'your_meta_key', true );
I think that wordpress built-in function about this would help you out even for the custom fields.
<p>Last modified: <?php the_modified_time(); ?></p>

Categories