I'm working on a plugin, where I have defined a hook which is supposed to create a custom post field in the admin panel. The code for the same:
//ADDING CUSTOM FIELDS IN ADMIN PANEL
add_action('add_meta_boxes', 'jericho_meta');
add_action('save_post', 'jericho_saved');
function jericho_meta()
{
add_meta_box('jericho_name', 'Favorite PPV', 'jericho_handler', 'post');
}
function jericho_handler()
{
$value = get_post_custom($post->ID);
$namey = esc_attr($value['jericho_name'][0]);
echo '<label for = "jericho_name">Favorite PPV</label><input type = "text" id = "jericho_name" name = "jericho_name" value = "'.$namey.'" />';
}
function jericho_saved()
{
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
{
return;
}
if(!current_user_can('edit_post'))
{
return;
}
if(isset($_POST['jericho_name']))
{
update_post_meta($post_id, 'jericho_name', $_POST['jericho_name']);
}
}
This code generates a custom post field in the admin post panel as shown in the screenshot below:
However, when I enter a value in that text field and click on Update, the input value never gets saved inside the text field when I try checking the field on refreshing the page.
What seems to be wrong with my code?
EDIT 1:
I have updated my code and added a new action named save_post and defined its corresponding function. However, the problem seems to be with the way I have defined the input field itself, because when I tried inspecting the text field's element, this is what I got:
<input type="text" id="jericho_name" name="jericho_name" value>
Based on the code you provided i can see that you are not registering callback/trigger for saving post meta data.
You need to handle this by yourself (will not be handled automaticly).
Currently, what you did is you tied "jericho_handler()" function to render data when post edit page renders. And that works just fine, as it should.
You need to add additional function which will be triggered on 'save_post' where you will handle saving data into the database.
add_action( 'save_post', 'cd_meta_box_save' );
function cd_meta_box_save( $post_id )
{
// Bail if we're doing an auto save
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
// if our nonce isn't there, or we can't verify it, bail
if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
// if our current user can't edit this post, bail
if( !current_user_can( 'edit_post' ) ) return;
// now we can actually save the data
$allowed = array(
'a' => array( // on allow a tags
'href' => array() // and those anchors can only have href attribute
)
);
// Make sure your data is set before trying to save it
if( isset( $_POST['my_meta_box_text'] ) )
update_post_meta( $post_id, 'my_meta_box_text', wp_kses( $_POST['my_meta_box_text'], $allowed ) );
if( isset( $_POST['my_meta_box_select'] ) )
update_post_meta( $post_id, 'my_meta_box_select', esc_attr( $_POST['my_meta_box_select'] ) );
// This is purely my personal preference for saving check-boxes
$chk = isset( $_POST['my_meta_box_check'] ) && $_POST['my_meta_box_select'] ? 'on' : 'off';
update_post_meta( $post_id, 'my_meta_box_check', $chk );
}
?>
Please check detail tutorial over here. Code i pasted above is from this link.
If you are already registering this handler and you are still encountering issues, please update your question with other parts of code as well.
Related
I've been googling and searching here for a simple way to add an empty field box to the Edit Orders page. We will use this to put in a reference for the shipment from our courier.
We would add it to the order notes, but we want it to be searchable, and also want to add it as a column in the Orders Admin page (I have Admin Columns plugin that I think can do that bit, I just need to add this field to start).
Hope someone can help, thanks!
EDIT:
I found this question which seems to be similar, but more complicated, than what I am looking for and I cant figure out how to simplify it. Add a custom meta box on order edit pages and display it on the customer order pages
I don't need anything that will display on the front end to customers like this. Just a simple empty box that displays on every order-edit page (perhaps below the order notes) that can be searched. I will then display it in a column on the order admin page too.
Managed to get an answer, this is working great!
//from::https://codex.wordpress.org/Plugin_API/Action_Reference/manage_posts_custom_column
// For displaying in columns.
add_filter( 'manage_edit-shop_order_columns', 'set_custom_edit_shop_order_columns' );
function set_custom_edit_shop_order_columns($columns) {
$columns['custom_column'] = __( 'Custom Column', 'your_text_domain' );
return $columns;
}
// Add the data to the custom columns for the order post type:
add_action( 'manage_shop_order_posts_custom_column' , 'custom_shop_order_column', 10, 2 );
function custom_shop_order_column( $column, $post_id ) {
switch ( $column ) {
case 'custom_column' :
echo esc_html( get_post_meta( $post_id, 'custom_column', true ) );
break;
}
}
// For display and saving in order details page.
add_action( 'add_meta_boxes', 'add_shop_order_meta_box' );
function add_shop_order_meta_box() {
add_meta_box(
'custom_column',
__( 'Custom Column', 'your_text_domain' ),
'shop_order_display_callback',
'shop_order'
);
}
// For displaying.
function shop_order_display_callback( $post ) {
$value = get_post_meta( $post->ID, 'custom_column', true );
echo '<textarea style="width:100%" id="custom_column" name="custom_column">' . esc_attr( $value ) . '</textarea>';
}
// For saving.
function save_shop_order_meta_box_data( $post_id ) {
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Check the user's permissions.
if ( isset( $_POST['post_type'] ) && 'shop_order' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_shop_order', $post_id ) ) {
return;
}
}
// Make sure that it is set.
if ( ! isset( $_POST['custom_column'] ) ) {
return;
}
// Sanitize user input.
$my_data = sanitize_text_field( $_POST['custom_column'] );
// Update the meta field in the database.
update_post_meta( $post_id, 'custom_column', $my_data );
}
add_action( 'save_post', 'save_shop_order_meta_box_data' );
Use the default WooCommerce custom field in the order edit page bottom section.
I have created form for enquiry. When user fill this form then I am saving it into database as a custom post type. In backend I need to show extra information on custom post edit page. But did not find any hook.
I have tried this code:
[![function add_extra_info_column( $columns ) {
return array_merge( $columns,
array( 'sticky' => __( 'Extra Info', 'your_text_domain' ) ) );
}
add_filter( 'manage_posts_columns' , 'add_extra_info_column' );][1]][1]
But it adds a new column in custom post.
I need to show extra info when we click on edit page link for every post.
This is just an example and you have to customize it for your needs:
First step: Adding Meta container hook to backend (for example here for products post type):
add_action( 'add_meta_boxes', 'extra_info_add_meta_boxes' );
if ( ! function_exists( 'extra_info_add_meta_boxes' ) )
{
function extra_info_add_meta_boxes()
{
global $post;
add_meta_box( 'extra_info_data', __('Extra Info','your_text_domain'), 'extra_info_data_content', 'product', 'side', 'core' );
}
}
(replace 'product' by your post type and this meta box could be like here on 'side' or by 'normal' to get it on main column)
Second step: Adding information in this metabox (fields, data, whatever…)
function extra_info_data_content()
{
global $post;
// Here you show your data <=====
}
References:
function add_meta_box()
How to add custom fields for WooCommerce order page (admin)?
How to add option to woo commerce edit order page?
Third step (optional): Save the data of the custom meta post (needed if you have some fields).
add_action( 'save_post', 'extra_info_save_woocommerce_other_fields', 10, 1 );
if ( ! function_exists( 'extra_info_save_woocommerce_other_fields' ) )
{
function extra_info_save_woocommerce_other_fields( $post_id )
{
// Check if our nonce is set.
if ( ! isset( $_POST[ 'extra_info_other_meta_field_nonce' ] ) )
{
return $post_id;
}
$nonce = $_REQUEST[ 'extra_info__other_meta_field_nonce' ];
//Verify that the nonce is valid.
if ( ! wp_verify_nonce( $nonce ) )
{
return $post_id;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
{
return $post_id;
}
// Check the user's permissions.
if ( 'page' == $_POST[ 'post_type' ] )
{
if ( ! current_user_can( 'edit_page', $post_id ) )
{
return $post_id;
}
}
else
{
if ( ! current_user_can( 'edit_post', $post_id ) )
{
return $post_id;
}
}
/* --- !!! OK, its safe for us to save the data now. !!! --- */
// Sanitize user input and Update the meta field in the database.
}
}
I have a problem, I want to update a custom user meta data field using another custom post field. It has to happen AFTER the whole post INCLUDING CUSTOM FIELDS saves / updates. But all the calls I find: update the basic post data, then call the action, THEN update the custom fields.
saved_post & post_updated cause this code to be delayed by 1 saved. ie, if I were to make a new post and set the $my_value to 5, first time I saved it would come back with 0, then the next time it would come back with 5. ect ect.
Does anyone know of an action hook that runs after custom post data has been saved? or how to make save_post or post_updated to fire after the custom post data?
function zhu_li_do_the_thing( $post_id ) {
//get post data, extract author-ID and post-Type
$post = get_post( $post_id );
$post_type = $post->post_type;
$userid = $post->post_author;
//if your my custom post type, then get the custom field value i want.
if( 'my_custom_post_type' == $post_type ){
$post_custom_fields = get_post_custom($post_id);
$my_custom_field = $custom_fields['im_a_field'];
foreach($im_a_field as $key ){
$my_value = $key;
}
// if the value starts with a "+" or "-" do math with it against authors-custom-metadata, if it's null ignore it, if it's anything else, make the metadata = the value.
if(substr($my_value, 0,1)=='+' || substr($my_value, 0,1)=='-'){
$my_int = intval($my_value);
$author_int = intval(get_user_meta($userid, 'the_objective, true));
$result = ($author_int + $str);
update_user_meta( $userid, 'the_objective', $result );
}elseif($my_value == null ){
return;
}else{
update_user_meta( $userid, 'the_objective', $my_value )
}}};
add_action( 'save_post, 'zhu_li_do_the_thing' );
You should use pre_post_update to check the post meta before and use post_updated to check the updated post meta.
The following is a snippet of code from a class that checks the stock status of a woocomerce product.
function __construct()
{
add_action( 'post_updated', array( $this, 'post_updated' ), 1, 1 );
add_action( 'pre_post_update', array( $this, 'pre_post_update' ), 1, 1 );
}
function pre_post_update( $post_ID )
{
global $post_type;
if( $post_type == 'product' )
define( 'stock_status_previous', get_post_meta( $post_ID, '_stock_status', 1 ) );
}
function post_updated( $post_ID )
{
if( defined( 'stock_status_previous' ) )
{
$stock_status = get_post_meta( $post_ID, '_stock_status', 1 );
if( $stock_status == stock_status_check )
return;
// post meta is different - sp do something
}
What i am actually trying to do here is :
- I have a custom post type to add videos.
- Now in every video a user can add multiple fields for a video for eg.
-- I have a video on which there will be multiple overlays containing (title, desc, button , link etc) and the number of overlays are completely dynamic so the user should be able to set it for every video.
-- So if the user says 5, then the user should be get 5 title, desc, button fields to add
I can add this statically for a particular amount, but how do i make this dynamic.
Thanks
You can create your own fields in your plugin, it is not difficult, you just need to know what you want to put in your post type ...
To add your fields you need to know jQuery to add more fields dynamically...
No need for another plugin created because you'd have many things that would use...
<?php
add_meta_box( "my-new-meta", "Video Options", "metas_form", "your_post_type_name", "normal", "high")
function metas_form( $post ){
$post_id = $post->ID;
$meta_value = get_post_meta( $post_id, 'meta_key_text', true );
?>
<div>
<label id="my_field">Name</label>
<input type="text" id="my_field" name="my_field" value="<?php echo $meta_value ?>">
</div>
<input type="hidden" name="field_nonce" id="field_nonce" value="<?php echo wp_create_nonce( plugin_basename(__FILE__) ); ?>" />
<?php
}
add_action( 'save_post', 'save_fn' );
function save_fn( ) {
global $post, $post_id, $typenow;
if ( $typenow == 'your_post_type_name' && isset( $_POST ) ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return $post_id;
if ( !isset( $_POST['field_nonce'] ) || !wp_verify_nonce( $_POST['field_nonce'], plugin_basename(__FILE__) )) return $post_id;
$meta_value = $_POST['my_field'];
update_post_meta( $post->ID, 'meta_key_text', $meta_value );
}
}
I've been trying to get my custom meta box data to save in Wordpress, and I haven't had any luck. I've tried researching other posts, but since everyone does it a little bit differently, I haven't had any success at using the tutorials and other posts out there.
I created a metabox:
add_action( 'add_meta_boxes', 'ic_add_heading_box' );
function ic_add_heading_box( $post ) {
add_meta_box(
'Meta Box',
'Heading Titles',
'ic_heading_box_content',
'page',
'normal',
'high'
);
}
function ic_heading_box_content( $post ) {
echo '<label>Main Heading (h1)</label>';
echo '<input type="text" name="heading_box_h1" value="" />';
echo '<label>Sub Heading (h3)</label>';
echo '<input type="text" name="heading_box_h3" value="" />';
}
I just can't for the life of me get the data I insert in to the fields to save in Wordpress. Any help would be greatly appreciated.
The function you are using is only a display function.
You are not actually doing nothing with the data . it is only for creating the metabox. not handling it.
You need to add
add_action( 'save_post', 'myplugin_save_postdata' );
and then use update_post_meta() with a function like in the codex example :
function myplugin_save_postdata( $post_id ) {
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
// Check the user's permissions. If want
if ( 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) )
return $post_id;
} else {
if ( ! current_user_can( 'edit_post', $post_id ) )
return $post_id;
}
/* OK, its safe for us to save the data now. */
// Sanitize user input. if you want
$mydata = sanitize_text_field( $_POST['myplugin_new_field'] );
// Update the meta field in the database.
update_post_meta( $post_id, '_my_meta_value_key', $mydata ); // choose field name
}