John Davidson

How to get tag_ID in WordPress functions.php and use it in function?

0 comments
Message:


I'have some problem with my WordPress functions.php script.


When I go to edit Category of Post, I have URL:


/wp-admin/term.php?taxonomy=category&tag_ID=3&post_type=post&wp_http_referer=%2Fwp-admin%2Fedit-tags.php%3Ftaxonomy%3Dcategory

I need a function to update the field in category with ID from URL.


function overwrite_ratings_category(){
update_field('field_630dbc7cf3fdd', '432', 'category_'.$_GET['tag_ID']);
}

add_action('edit_term','overwrite_ratings_category');

When I use number (e.g. 3) instead $_GET['tag_ID'] function works fine. But it's not working with $_GET['tag_ID'].


Is there any other way to get the ID of the currently edited category?



My Answer:
To get the tag_ID in WordPress functions.php and use it in a function, you can use the following code:

php
function overwrite_ratings_category($term_id){
if(isset($_GET['tag_ID'])){
$tag_id = $_GET['tag_ID'];
update_field('field_630dbc7cf3fdd', '432', 'category_'.$tag_id);
}
}

add_action('edit_term','overwrite_ratings_category');


In this code, we are using the `edit_term` action hook to trigger the `overwrite_ratings_category` function when a term (category) is being edited. The function checks if the `tag_ID` parameter is set in the URL, and if it is, it retrieves the value and uses it to update the field in the category with the specified ID.

Make sure to replace `'field_630dbc7cf3fdd'` with the actual field key you want to update, and `'432'` with the value you want to set for that field.

This should allow you to get the tag_ID from the URL and use it in your function to update the category field accordingly.

Rate this post

5 of 5 based on 4421 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us