simple condition on wordpress function.php not work - php

i want add a new field "title" on comment of wordpress, after insert the new input field in a default form of wordpress i added this in my function.php for save the title when new comment are submitted.
this is the code i use for save title:
function add_comment_meta_values($idcommento) {
global $post;
$idcommento= get_comment_ID();
$tipodipost= get_post_type($post->ID);
if( get_post_type($post->ID) == 'service') {
if(isset($_POST['title_svz']) ) {
$title= wp_filter_nohtml_kses($_POST['title_svz']);
add_comment_meta( $idcommento , 'title_svz', $title, false);
}}
}
add_action ('comment_post', 'add_comment_meta_values', 1);
this code work only when remove the condition :
if( get_post_type($post->ID) == 'service') {}
and i don't understand why, i have already tried this condition in comment.php or in a footer with simple function like this
function test_function() {
if( get_post_type($post->ID) == 'service') { echo 'done'; }
}
add_action( 'wp_footer', 'test_function' );
and it's work, so i don't understand why don't work in my primary code, any idea ?
SOLVED MYSELF
THIS IS THE NEW CODE:
function add_comment_meta_values($idcomment) {
$comment_full = get_comment( $idcomment );
$idpost = $comment_full->comment_post_ID;
$typepost= get_post_type($idpost);
if( $typepost == 'service') {
if(isset($_POST['title_svz']) ) {
$title= wp_filter_nohtml_kses($_POST['title_svz']);
add_comment_meta( $idcomment , 'title_svz', $title, false);
} }
}
add_action ('comment_post', 'add_comment_meta_values', 10, 1);

Sometimes in Wordpress, depending on the Context, the global $post may give you unexpected result. Thus something like $post->ID may not point to the appropriate ID you are looking for. You may as well try inspecting the $post object to see if it is what you had expected; like so:
<?php
function add_comment_meta_values($idcommento) {
global $post;
// TRY DUMPING THE $post VARIABLE TO SEE WHAT YOU GET
var_dump($post->ID);
var_dump($post->post_type);
var_dump($post);
exit;
$idcommento = get_comment_ID();
$tipodipost = get_post_type($post->ID);
// ALTHOUGH THIS IS ESSENTIALLY THE SAME AS WHAT YOU DID
if( $post->post_type == 'service') {
if(isset($_POST['title_svz']) ) {
$title= wp_filter_nohtml_kses($_POST['title_svz']);
add_comment_meta( $idcommento , 'title_svz', $title, false);
}}
}
add_action ('comment_post', 'add_comment_meta_values', 1);
After the Inspection, you'd sure know where and what was not OK in your Code...

Related

Woocommerce - display different product thumbnails on category page

I would like to display a different thumbnail for some of my products, if they appear in specific categories.
I have removed the current thumbnails from the specific category and I am using a custom field to pull the new thumbnail for the products I want to replace, which is working great. However, when I try to call the normal thumbnail for the remaining products it doesn't work - any ideas?
add_action( 'woocommerce_before_main_content', 'remove_test_category_thumbnails', 10 );
function remove_test_category_thumbnails() {
if (is_product_category('test-category')) {
remove_action('woocommerce_before_shop_loop_item_title' , 'woocommerce_template_loop_product_thumbnail' , 10);
}
}
add_action( 'woocommerce_before_shop_loop_item_title', 'add_test_category_thumbnails', 10 );
function add_test_category_thumbnails() {
global $post;
$testCatThumb = get_post_meta( $post->ID, 'step_dad_mug', true);
if (is_product_category('test-category') && (isset($testCatThumb)) ) {
echo wp_get_attachment_image( $testCatThumb );
}
else
echo woocommerce_get_product_thumbnail();
}
}
You need to echo the woocommerce_get_product_thumbnail. check below code.
add_action( 'woocommerce_before_main_content', 'remove_test_category_thumbnails', 10 );
function remove_test_category_thumbnails() {
if (is_product_category('test-category')) {
remove_action('woocommerce_before_shop_loop_item_title' , 'woocommerce_template_loop_product_thumbnail' , 10);
}
}
add_action( 'woocommerce_before_shop_loop_item_title', 'add_test_category_thumbnails', 10 );
function add_test_category_thumbnails() {
global $post;
$testCatThumb = get_post_meta( $post->ID, 'step_dad_mug', true);
if( is_product_category( 'test-category' ) && ( isset( $testCatThumb ) ) ) {
echo wp_get_attachment_image( $testCatThumb );
}else{
echo woocommerce_get_product_thumbnail();
}
}
The following function expects a post id, is that what you are passing it?
wp_get_attachment_image();
as part of your if condition, you could check the value is of the correct value type:
is_numeric($testCatThumb);
I tend to create readable variables, rather than having horrible conditions you have to parse with your brain every time you read it:
function add_test_category_thumbnails() {
global $post;
$testCatThumb = get_post_meta( $post->ID, 'step_dad_mug', true);
$isPostId = !empty($testCatThumb) && is_numeric($testCatThumb);
$isValidAttachmentId = is_product_category('test-category') && $isPostId;
$image = $isValidAttachmentId ? wp_get_attachment_image($testCatThumb) : '';
// display custom image, or fallback to woocommerce thumbnail
echo !empty($image) ? $image : woocommerce_get_product_thumbnail();
}

Adding post meta by URL in wp-admin

I have a script that order Wordpress posts by custom field and I would like to have a link that add's a custom field by clicking on it.
Like this (or so): <a href="/wp-admin/post.php?action=add_post_meta&my_meta_key&my_meta_value&post=1500&_wpnonce=...
My script uses:
$path = preg_replace('/wp-content.*$/','',__DIR__);
include($path.'wp-load.php');
global $wpdb;
Is this possble?
You don't need to look for any meta in post. You can simply do this on your single.php:
if( current_user_can('manage_options')) {
add_post_meta($post->ID, 'new_meta', 1, true);
}
You can also add this on functions.php
add_action('insert_post_meta', 'adding_new_custom_field');
function adding_new_custom_field ($post_id)
{
if ( $_POST['post_type'] == 'post' ) {
add_post_meta($post_id, 'new_meta_key_name', 'new meta value', true);
}
return true;
}
Why don't you script something like this:
if( current_user_can('manage_options')) {
// Check and get any post meta
$post_meta = get_post_meta( $post->ID, 'post_meta', true );
if (!empty($post_meta)){
add_post_meta($post->ID, 'new_meta', 1, true);
}
}
Now you can place this on your single.php, and... et voilĂ !
When you open the post, the new meta will be added.

WordPress Metabox won't save

I was trying to make my metabox work but for some reason whenever I put some text and try to save the data inside the textarea it wont save.
add_action("admin_init", "custom_product_metabox");
function custom_product_metabox(){
add_meta_box("custom_product_metabox_01", "Product Description", "custom_product_metabox_field", "portfolio_page", "normal", "low");
}
function custom_product_metabox_field(){
global $page;
$data = get_post_custom($page->ID);
$val = isset($data['custom_product_input']) ? esc_attr($data['custom_product_input'][0]) : 'no value';
echo '<textarea rows="5" cols="220" name="custom_product_input" id="custom_product_input" value="'.$val.'"></textarea>';
}
add_action("save_post", "save_detail");
function save_detail(){
global $page;
if(define('DOING_AUTOSAVE') && DOING_AUTOSAVE){
return $page->ID;
}
update_post_meta($page->ID, "custom_product_input", $_POST["custom_product_input"]);
}
This is actually a code for a portfolio page where I embedded inside the functions.php. Any idea how can I make it work and save the data?
Thanks!
Your save method looks wrong. Try something like
function custom_product_metabox_field($post){
//global $page; remove this line also
$data = get_post_custom($post->ID);
$val = !empty(get_post_meta( $post->ID, 'custom_product_input', true )) ?
get_post_meta( $post->ID, 'custom_product_input', true ) : 'no value';
echo '<textarea rows="5" cols="220" name="custom_product_input" id="custom_product_input" value="'.$val.'"></textarea>';
}
add_action("save_post", "save_detail", 10, 3 );
function save_detail($post_id, $post, $update){
//global $page;// remove this line
if(define('DOING_AUTOSAVE') && DOING_AUTOSAVE){
return $post_id;
}
update_post_meta($post_id, "custom_product_input", $_POST["custom_product_input"]);
}

get post meta wordpress not working in plugin

EDIT: More Code.
Problem: I want to get the post meta of a post. It works fine for the case updated_post, but not for new_post and I just can't figure out why..
This is the function for the cases:
function userpro_sc_new_post( $new_status, $old_status, $post ) {
global $userpro_social;
$exclude = userpro_sc_get_option('excluded_post_types');
if ($exclude != ''){
$exclude_types = explode(',',$exclude);
} else {
$exclude_types = array('nav_menu_item');
}
if (!in_array($post->post_type, $exclude_types )) {
// new post
if ( $new_status == 'publish' && $old_status != 'publish' ) {
$user = get_userdata($post->post_author);
$userpro_social->log_action( 'new_post', $user->ID, $post->ID, $post->post_title, $post->post_type );
}
// updated post
if ($new_status == 'publish' && $old_status == 'publish' ){
$user = get_userdata($post->post_author);
$userpro_social->log_action( 'update_post', $user->ID, $post->ID, $post->post_title, $post->post_type );
}
}
}
And this is the code to run in the cases:
function log_action($action, $user_id, $var1=null, $var2=null, $var3=null) {
global $userpro, $userpro_social;
$activity = get_option('userpro_activity');
$timestamp = current_time('timestamp');
$status = '';
switch($action){
case 'new_post':
$myId = get_post_meta(get_the_ID(), 'wpex_post_video_oembed', true);
$status .= $myId;
break;
case 'update_post':
$myId = get_post_meta(get_the_ID(), 'wpex_post_video_oembed', true);
$status .= $myId;
break;
}
Like I said, update_post works so I can see the ID... new_post does not work. Why?
I simplified the code to run a bit, but it is still the same issue.
Please help!
You have to be aware of three things before using get_post_meta() in your plugins.
You must declare global variables as global if any (eg: $wpdb).
You have to get the post data in $post_id (eg: $post_id = $_POST['postid'];).
Update the custom field value if needed (eg: update_post_meta($post_ID, 'video_id', true);).
Any of the above could be your problem. Please refer and try.
global $post;
$avriable_name=get_post_meta($post->ID, 'video_id', true);
Try the above code, global post will help to get the id for the post. If you didnt decalre it $post->ID will be blank and rest will not work.
Please let me know if you need further help.
Try this:
$myId = (get_post_meta(get_the_ID(), 'wpex_post_video_oembed',true));

How to get access to post ID when using save_post action for WordPress

So I have this code that basically adds a post meta field when a mirror is created. The only issue i'm having is how I can access to the post id of the mirror that was just created? I've got the following code from this link: http://codex.wordpress.org/Plugin_API/Action_Reference/save_post that has sample code, and adjusted it to my needs. The mirrors are created on the front end using a form. Using the code below, the post meta field values are not added to the mirrors when they are created. This is probably because $post_id doesn't hold anything.
From the link provided above, it says I have access to $_GET and $_POST, which one should I use and how do I get the ID of the post? Should I just use $_POST->ID?
// Order mirrors when they are created
function order_mirror_create($post_id) {
$slug = 'mirror';
if ( $slug != $_POST['post_type'] ) {
return;
}
if ($videohost == "UploadAnime") {
add_post_meta(get_the_ID(), 'video_display_order', 1, true);
} else {
add_post_meta(get_the_ID(), 'video_display_order', time(), true);
}
}
add_action( 'save_post', 'order_mirror_create' );
EDIT: Updated my code using $post->ID and added global $post, however, this still does not work.
// Order mirrors when they are created
function order_mirror_create($post_id) {
global $post;
$slug = 'mirror';
if ( $slug != $_POST['post_type'] ) {
return;
}
if ($videohost == "UploadAnime") {
add_post_meta($post->ID, 'video_display_order', 1, true);
} else {
add_post_meta($post->ID, 'video_display_order', time(), true);
}
}
add_action( 'save_post', 'order_mirror_create' );
EDIT 2: Defined $videohost and used $post_id, however, still not working.
// Order mirrors when they are created
function order_mirror_create($post_id) {
$slug = 'mirror';
if ( $slug != $_POST['post_type'] ) {
return;
}
$videohost = get_post_meta($post_id, 'video_provider', true);
if ($videohost == "UploadAnime") {
add_post_meta($post_id, 'video_display_order', 1, true);
} else {
add_post_meta($post_id, 'video_display_order', time(), true);
}
}
add_action( 'save_post', 'order_mirror_create' );

Categories