Your check is incorrect: right now, it tries to access $_POST['rating']
, which might not be set, and then checks whether that value is empty. Furthermore, you should scope your if
statement with brackets, because right now, your code will try and update the average rating even if no new rating is provided. Try:
function save_comment_meta_rating( $comment_id ){
if ( ! array_key_exists( 'rating', $_POST ) || empty( $_POST['rating'] ) ) {
return;
}
$rating = sanitize_text_field( $_POST['rating'] );
add_comment_meta( $comment_id, 'rating', $rating );
// Then update the average rating.
update_post_avg_rating( $comment_id );
}
add_action( 'comment_post', 'save_comment_meta_rating' );
Related Posts:
- How to store the_title() into a variable to reutrn the value, not just echo it
- How do I make my function add variables/values to the $post object?
- Cleanest/Fastest way to avoid calling and retrieving data from the database multiple times?
- How do I know what variables are passed in a filter/action and what their meaning is?
- Count the number of times the search form template gets included on a page
- How to put a variable in a instance in the widget
- Get value from shortcode to do something
- Creating WordPress Shortcode with Variable
- Changing the order of custom fields in the dashboard for Woocommerce variable products [closed]
- How to reference PHP in Javascript
- Overide Variable in Child Theme
- How to send a php var to jQuery Ajax call with two different file
- Custom global variable not working in function
- Best way to use variables in multiple functions?
- Trying to add some custom text into WordPress Post title via function.php
- Pulling a variable into the wp_nav_menu function
- Where can I find the declaration of `$_wp_theme_features`?
- Mega Menu Walker
- How to add a class to the attachment images
- How to call a function only once (global variable scope)
- Multiple is_page() in page.php
- Is it possible to get a theme customizer setting from wp.customize using jquery?
- How to correct schedule my event weekly with wp_schedule_event()
- Execute a shortcode when clicking on a image
- add_rewrite_rule with bottom priority doesn’t handle the WordPress pages
- How can update custom meta for all posts
- Change logo based on incoming domain
- wrong php date()?
- How do I fetch all comments per post via WP REST API?
- How do I display a user’s previous orders as a select box option on a product?
- Remove meta description on certain pages
- Image media upload metabox
- How to combine wordpress_logged_in cookies in one cookie?
- Two Different Links for Same Product – WooCommerce [closed]
- Woocommerce related product text
- esc_html_e() is not translating string in wordpress [closed]
- Pass php dynamic variable to shortcode
- Limit 1 global comment per minute
- Modify custom field input data before creating a new post
- Query Pages and post excerpts dynamically
- Is there a way to conditionally check whether a WordPress post title is empty?
- Echo a function inside a function
- Use WordPress function in php file
- Get my site session in wordpress?
- is_user_logged_in() not working in AJAX validation call
- Register a menu – Error Header
- How to keep the capability of users and disable Gutenberg editor in WordPress?
- Add product to cart for user – WC()->cart->add_to_cart [closed]
- How to create a field in customize and show that in header.php?
- Set site title & tagline with wp.config or function.php
- Shortcode to embed Edit Account form not working
- How to make jquery count down timer function manually editable
- Hierarchical taxonomy list with modificated term links
- Enqueing External JS on the remote server JS
- iframe not showing in content on blog page
- Get user custom field value on function.php
- How Do I Find & Remove H1 From Site-Title-Wrapper? [closed]
- Get the name of menu item with wp_nav_menu
- PHP Output also in Child theme, but different
- Dynamically Generate Functions and Hooks
- Strip from or something better?
- Customize field names in backend profile edit page through function.php [duplicate]
- Time Delay a URL Redirect for Specific Page
- Isn’t Returning Value While Using SELECT COUNT(*) FROM {$wpdb->prefix}
- Need help for some PHP code
- Unable to display multiple parameters from url by javascript through shortcodes
- Adding HTML Code to Replace Text in PHP
- how to replace h1 entry title with h2 in category pages only
- How to unset a function from a action inside a class, that is called in another class
- Run a sql (update) after 12 hours after the user login. Woocommerce users
- Wp_Schedule_Event every few minutes doesn’t work
- File from parent theme imported to child theme doesn’t work – any ideas?
- Problem with displaying CSS Stylesheets – Am I adding them correctly in my wordpress child theme?
- register_block_type is not working properly
- PHP “warning include_once(): Failed to open stream” Simple HTML DOM in WordPress Child Theme
- How to override WooCommerce Order Item Meta with data from Cart?
- Taxonomy Child Term, Counter is staying on 0
- Access to “My Site” is missing from the admin bar
- How to redirect (301) trashed post to it’s parent taxonomy TERM ARCHIVE instead of 404 page
- Offset with ajax load more posts duplicates
- Need help with PHP functions
- Using get_theme_mod with checkbox to display content
- WordPress styling recent post
- Styling admin page rows in order of importance (checkboxes)
- Break a WordPress function to run in patches and re-continue
- Pagenav Not appearing on custom Template
- syntax error unexpected ‘}’ at 364 line
- get_post_meta no longer working
- Highlight posts currently being edited on multiple editor site?
- My title is showing after the shortcode
- Load a Header in wordpress
- Adding multiple conditional tags in a function?
- Assign new post author IF another user in custom field on post transition
- Create and publish a new post every Monday, listing the post titles and links to the posts published during the previous week
- How do I change the Go To Categories link in the term_updated_messages
- WooCommerce – Show ‘Sales’ badge on variable products where only some variations have sales price [closed]
- Why a class property becomes empty?
- Hide category by tag
- How do I stylize selected comments?
- add_rewrite_rule image from /images/site2/favicon.ico to /favico.ico