Custom Meta Field Has Stopped Working - wordpress

I have a custom meta checkbox in a custom post type that until this afternoon was working fine, I went into add some new post types and the checkbox won't save I did add the isset value in the show_meta but it was still working after I did this and does not start working again if I remove it, and nothing is showing up in my debug can anybody help?:
function add_tiles_meta_box() {
add_meta_box(
'tiles_meta_box', // $id
'Show in Large Tiles?', // $title
'show_tiles_meta_box', // $callback
'tiles', // $screen
'normal', // $context
'high' // $priority
);
}
add_action( 'add_meta_boxes', 'add_tiles_meta_box' );
function show_tiles_meta_box( $post, $metabox ) {
$tiles = get_post_meta( $post->ID, 'tiles', true );
wp_nonce_field( basename(__FILE__), 'tiles_meta_box_nonce' );
?>
<!-- All fields will go here -->
<p>
<input type="checkbox" name="tiles" <?php echo (isset($_POST['tiles'])?"value='on'":"value='off'")?> <?php checked( $tiles, 'on' ); ?>/> Yes this is a large tile.
</p>
<?php
}
$post_ID = $post->ID;
function save_tiles_meta($post_ID) {
$post_ID = (int) $post_ID;
$post_type = get_post_type($post_ID);
$post_status = get_post_status( $post_ID );
if ($post_type) {
update_post_meta($post_ID, "tiles", $_POST["tiles"]);
}
return $post_ID;
}
add_action( 'save_post', 'save_tiles_meta' );

Your save_tiles_meta function is unnecessarily convoluted. Something may be failing along the way. Here's a more standard way to do that:
add_action( 'save_post', 'save_tiles_meta' );
function save_tiles_meta(){
global $post;
update_post_meta( $post->ID, 'tiles', $_POST['tiles'] ); // On or Off
}

Try with below code
add_action( 'save_post', 'save_tiles_meta',99,1 );
function save_tiles_meta($postId){
global $post;
update_post_meta( $post->ID, 'tiles', $_POST['tiles'] ); // On or Off
}

Related

custom field value is blank

function add_custom_fields($post_id) {
global $post;
$metadescription = wp_trim_words( get_the_content(), 55 );
add_post_meta( $post_id, 'meta_description', $metadescription, true );
}
add_action( 'wp_insert_post', 'add_custom_fields' );
what i going wrong?
Below is a code that will add_post_meta every time a post or page is created or updated on your website.
function add_custom_fields($post_id) {
// If this is a revision, don't add content.
if ( wp_is_post_revision( $post_id ) )
return;
global $post;
$metadescription = wp_trim_words( apply_filters('the_content', $post->post_content), 55, '');
add_post_meta($post_id, 'meta_description',$metadescription, true);
}
add_action( 'wp_insert_post', 'add_custom_fields', 10, 1 );

Notice: Trying to get property of non-object On WordPress save_post acion hook

I am creating a plugin where I need to update the input field post meta when the actual post is updated. So here is my code:
function save_meta_function ( ) {
global $post;
$post_id = $post->ID;
$meta_values = get_post_meta( $post_id );
foreach ($meta_values as $key => $value) {
update_post_meta( $post_id, $key, $_POST[$key] );
}
}
add_action( 'save_post', 'save_meta_function' );
but it's showing several errors:
Notice: Trying to get property of non-object // that's $post_id
= $post->ID;
Can you tell me why $post_id = $post->ID; line showing that error?
Replace your code with follows -
function save_meta_function ( $post_id ) {
$meta_values = get_post_meta( $post_id );
foreach ($meta_values as $key => $value) {
update_post_meta( $post_id, $key, $_POST[$key] );
}
}
add_action( 'save_post', 'save_meta_function' );

Custom Post Type Metabox not saving data

I'm creating a custom post type that will have a custom metabox with a new field. For some reason, my field isn't saving data. Attached is my code:
function route_coordinates(){
add_meta_box(
'coordinate_box',
'Route Coordinates',
'route_coordinates_html',
'routes'
);
}
add_action('add_meta_boxes', 'route_coordinates');
function route_coordinates_html(){
?>
<textarea name="route_coordinates" id="route_coordinates" class="widefat"><?php echo esc_attr( get_post_meta( $post->ID, 'routes_coordinates', true ) ); ?></textarea>
<?php
}
function map_save_postdata($post_id)
{
if (array_key_exists('route_coordinates', $_POST)) {
update_post_meta(
$post_id,
'_coordinates',
$_POST['route_coordinates']
);
}
}
add_action('save_post', 'map_save_postdata');
Got it fixed, was using the wrong commands.
function route_coordinates(){
add_meta_box(
'coordinate_box',
'Route Coordinates',
'route_coordinates_html',
'routes'
);
}
add_action('add_meta_boxes', 'route_coordinates');
function route_coordinates_html(){
global $post;
$get_all_meta_values = get_post_custom($post->ID);
$route_coordinates=$get_all_meta_values["route_coordinates"][0];
?>
<textarea name="route_coordinates" id="route_coordinates" class="widefat"><?php
echo $route_coordinates; ?></textarea>
<?php
}
add_action('save_post', 'save_route_coordinates');
function save_route_coordinates(){
global $post;
update_post_meta($post->ID, "route_coordinates", $_POST["route_coordinates"]);
}

Show Custom Fields under "Add to Cart" button

I have using this function code to make new Custom Text Area, but have issue to show it in product page.
// Custom Field Product
add_action( 'woocommerce_product_options_general_product_data',
'woo_add_custom_general_fields' );
add_action( 'woocommerce_process_product_meta',
'woo_add_custom_general_fields_save' );
function woo_add_custom_general_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
woocommerce_wp_textarea_input(
array(
'id' => '_textarea',
'label' => __( 'Custom Text:', 'woocommerce' ),
'placeholder' => '',
'description' => __( 'Enter the custom value here.', 'woocommerce' )
)
);
echo '</div>';
}
// Save Changes to DB
function woo_add_custom_general_fields_save( $post_id ){
// Textarea
$woocommerce_textarea = $_POST['_textarea'];
if( !empty( $woocommerce_textarea ) )
update_post_meta( $post_id, '_textarea', esc_html( $woocommerce_textarea
) );
}
// Show data to product
add_action( 'woocommerce_after_add_to_cart_button',
'custom_content_after_addtocart_button', 100 );
function custom_content_after_addtocart_button() {
// custom content.
echo get_post_meta( $post->ID, '_textarea', true );
}
Looks like when press save, its storing data in DB, but its not showing in single product page. Can someone to tell me where is issue in this function?
The problem is that in the following function, $post is not defined. (The code was indented for clarity.)
// Show data to product
add_action( 'woocommerce_after_add_to_cart_button',
'custom_content_after_addtocart_button', 100 );
function custom_content_after_addtocart_button() {
// custom content.
echo get_post_meta( $post->ID, '_textarea', true );
}
So, a simple fix would be to add global $post; to the function:
// Show data to product
add_action( 'woocommerce_after_add_to_cart_button',
'custom_content_after_addtocart_button', 100 );
function custom_content_after_addtocart_button() {
global $post;
// custom content.
if ( $post ) {
echo get_post_meta( $post->ID, '_textarea', true );
}
}
Alternatively, you can use the global $product object:
// Show data to product
add_action( 'woocommerce_after_add_to_cart_button',
'custom_content_after_addtocart_button', 100 );
function custom_content_after_addtocart_button() {
global $product;
// custom content.
if ( $product ) {
echo get_post_meta( $product->get_id(), '_textarea', true );
}
}

Wordpress - Adding custom field to the post screen

I was wondering if its possible to add a little box, like the 'excerpt' box to all posts in wordpress so that i can enter a URL.
Cheers,
You can use the add_meta_box function. You also need a callback function that outputs the form html on the post screen and a save function.
Here is a basic example that adds a URL meta box to lower right hand side of the post screen.
add_action( 'add_meta_boxes', 'c3m_sponsor_meta' );
function c3m_sponsor_meta() {
add_meta_box( 'c3m_meta', 'Sponsor URL Metabox', 'c3m_sponsor_url_meta', 'post', 'side', 'high' );
}
function c3m_sponsor_url_meta( $post ) {
$c3m_sponsor_url = get_post_meta( $post->ID, '_c3m_sponsor_url', true);
echo 'Please enter the sponsors website link below';
?>
<input type="text" name="c3m_sponsor_url" value="<?php echo esc_attr( $c3m_sponsor_url ); ?>" />
<?php
}
add_action( 'save_post', 'c3m_save_project_meta' );
function c3m_save_project_meta( $post_ID ) {
global $post;
if( $post->post_type == "post" ) {
if (isset( $_POST ) ) {
update_post_meta( $post_ID, '_c3m_sponsor_url', strip_tags( $_POST['c3m_sponsor_url'] ) );
}
}
}
Edit: Corrected namespace error in code above.

Resources