Thanks for any help with this.
I need to know how to hook wp_insert_post (or whatever is similar and better?) without it firing multiple times. What is the correct way to do this in WordPress?
For example:
In Plugin.php
add_filter( 'wp_insert_post', 'add_data') );
...
function add_data()
{
// This line is outputted twice
terseErrorLog("This code was executed.");
}
Try this:
function add_data() {
global $post;
if ($post - > post_status == "publish") {
terseErrorLog("This code was executed.");
}
}
add_action('save_post', 'add_data');
Related
I need to add my ACF field to a custom function. I have tried this but there is syntax error.
add_action( 'flatsome_custom_single_product_1', function () {
echo <?php the_field('designer'); ?>;
} );
When you are calling the_field('designer') outside the actual post loop, you need to pass the post ID as well.
Here is the Ref.
For your case, you can achieve this by using the code like below
add_action( 'flatsome_custom_single_product_1', function () {
global $post;
echo get_field('designer', $post->ID);
} );
I want to execute a function in a specific page only, that edits the post title of posts inside a post-grid plugin.
I tried:
if (is_page('my-slug')) { add_filter(...); } //page slug
if (is_page('12345')) { add_filter(...); } //page ID
add_filter('the_title', 'title_with_attributes', 10, 2);
function title_with_attributes($title, $id) {
if (is_page('12345') || is_page('my-slug')) {
//Do stuff ...
}
}
With no results. The function is the following and runs fine.
function title_with_attributes($title, $id) {
global $wp_query;
if(get_post_type($post->ID)=="product"){
global $post;
//get other variables from post here ...
$title = "add text and vars ".$title;
}
return $title;
}
Maybe there is a way to add this function when the plugin runs but I do not know how. The plugin is the "Post-Grid" from WP Bakery
I want to add a custom url for the front page using YOAST. I'm trying this approach but it doesn't do anything, can you help me?
function design_canonical($url) {
global $post;
$url == ( 'www.cator.com/cator/');
}
add_filter( 'wpseo_canonical', 'design_canonical' );
any idea where is the problem?
thanks
You first need to check if your page is home or not which you can check through is_home()
function design_canonical($url) {
if (is_home()) {
return 'your-custom-url.com'; //Enter here your custom url
} else {
return $url;
}
}
add_filter( 'wpseo_canonical', 'design_canonical' );
I'm trying to give a pretty url to my custom CPT (Orders). This is the way im trying:
add_filter('post_type_link', 'custom_post_type_link', 1, 3);
function custom_post_type_link( $link, $post = 0 ){
if ( $post->post_type == 'orders' ){
$order_code = 1121000+$post->ID;
return home_url( 'orders/' . $order_code);
} else {
return $link;
}
}
Assume that post ID is 32, This code creates the following url:
http://www.example.com/orders/1121032
Now i wanna write a code to display the post with post ID=32 in above url.
I heard somewhere that i should use custom query vars. But i don't know how to use it.
This is the rest of code i've written
add_filter('query_vars', 'custom_add_query_vars');
function custom_add_query_vars($qVars){
$qVars[] = "code";
return $qVars;
}
add_action( 'init', 'custom_rewrites_init' );
function custom_rewrites_init(){
add_rewrite_rule(
'orders/([0-9]+)?$',
'index.php?post_type=orders&code=$matches[1]',
'top' );
}
After adding custom rewrite rules, wordpress needs to be told to activate the new rule.
Use this method after the add_rewrite_rule function call.
$wp_rewrite->flush_rules();
Warning: flush_rules is expensive, you definitely don't want to call it on every request. Typically you would put the custom_rewrites_init and flush_rules in a plugin register_activation_hook function.
If you're lazy, you can just add it to your code once, make a request to the website (which will rewrite the .htaccess rewrite rules), then comment the flush_rules method out.
I have the following code to update the post_meta when a post is created. It is very simple,just storing the its own post_id in a meta field(might add more in the future)
The following code is not working, I guess it is because the $post_ID is blank, how do I pass the post_id of newly created post to the function update_postmeta (in function.php)?
//code from function.php
add_action('save_post', 'update_postmeta');
function update_postmeta($post_ID) {
update_post_meta($post_ID, 'related_id',$post_ID);
}
Here is a good boilerplate for you:
function update_postmeta($post_id) {
global $post;
// Post meta isn't sent for autosaves
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
update_post_meta($post->ID, 'related_id', $rand_id);
}
No where in your code is $rand_id defined though.