Infinite loop error with publish_post and wp_insert_post - wordpress

I got the infinite loop error. I require to insert the post in German language when the post is created for default English language.
I used publish_post action hook for catch the english posting event. But the publish_post hook is also executed by wp_insert_post() fucntion while creating the German post. So the infiniter error occured. Could anyone help ? Thank you. Below is the code i have used.
add_action( 'publish_post', 'save_in_all_sites' );
function save_in_all_sites( $post_id ){
global $sitepress;
$my_post = array(
'post_title' => $post_title,
'post_content' => $post_content,
'post_status' => $post_status
);
$def_trid = $sitepress->get_element_trid($post_id);
$ru_post_id1 = wp_insert_post( $my_post );
// insert the post in German language
$sitepress->set_element_language_details($ru_post_id1, 'post_post', $def_trid, 'de');
}

It should work to remove the hook right before you wp_insert_post and then add it back right after.
Example
remove_action( 'publish_post', 'save_in_all_sites' );
$ru_post_id1 = wp_insert_post( $my_post );
add_action( 'publish_post', 'save_in_all_sites' );

where is the code for english language ? is it $def_trid ? if yes then you are setting both english and german languages to element_language_details so when you get it, it returns you everything that is in english and german language trying setting these up separately with separate columns in your database too.

Related

WP Ultimate Member hook/function not running

I am using the Ultimate Member plugin and trying to trigger things after the registration form is filled in successfully.
As a test, I am simply creating a new post if the hook is run: um_registration_complete.
https://docs.ultimatemember.com/article/1234-umregistrationcomplete
function my_registration_complete( $user_id, $args ) {
// Create post object
$my_post = array(
'post_title' => 'function working',
'post_content' => 'hello world.',
'post_status' => 'publish',
'post_author' => 1,
);
// Insert the post into the database
wp_insert_post( $my_post );
}
add_action( 'um_registration_complete', 'my_registration_complete', 10, 2 );
Nothing happens after a successful registration. No post.
I tried adding die(); as a test to break the site on purpose if the hook runs, still nothing.
How best to debug this issue, I see nothing wrong with how I am using the hook but still it's not running.
Figured out my issue, and something I should have mentioned originally too. I am using the roots.io framework.
Functions etc namespaced, my add_action function name was not namespaced. Here is the working line:
add_action( 'um_registration_complete', __NAMESPACE__ . '\\my_registration_complete', 10, 2 );

Update all posts - wordpress

I have looked at this thread and tried to implement the given code sample there;
//code snippet to mass update all posts
add_action('init','mass_update_posts');
function mass_update_posts(){
$all_posts = get_posts('numberposts=');
$my_posts = get_posts( array('post_type' => 'post', 'numberposts' => $all_posts ) );
foreach ( $my_posts as $my_post ):
wp_update_post( $my_post );
endforeach;
}
I put the code in my footer.php but it doesn't seem to do anything? What am I missing? Initially I used;
$my_posts = get_posts( array('post_type' => 'post', 'numberposts' => -1 ) );
But that didn't help either...
Let me see if I understand what you are trying to do with this code...
First every time someone loads the page the footer.php will fire so you want to mass update all of your posts with a loop of the post itself?
There about a million things wrong with what you are trying to do with this code.
Never add actions in a footer file they belong in the function.php file of a theme.
Your $all_posts variable is probably empty because you are sending a function expecting an array of arguments a string (please read the get_post() function documentation)
$all_posts is not an integer as you are using it the next line (the get_post() function returns a list of WP_Post objects.
Your loop goes through all your posts and updates them with the same post, changing nothing and effectively accomplishing nothing.
So I guess the real question is what exactly are you trying to accomplish?

Wordpress Profile File / Page

I would like to create a file in the Wordpress theme, where i will add my own code, edit profile, show profile information, and perhaps an ability to insert posts / meta data programmatically.
So it needs to be www.mysite.com/profile.php or www.mysite.com/profile/
I do not want to use Buddy Press or any other plugin.
I know how the template system works, i do not want a page template.
It will probably be a class, later on, i do not want to change .htaccess file, and if i must i would appreciated filter function how to do this from functions.php
Basically just a simple .php file i can link to, located in theme root.
include('../../../wp-load.php');
and write any code i would like to.
Any creative solution that is not too "hacky" would be appreciated.
Spent around 2 days googling bashing my head on this, before i decided to ask question.
Thank you very much.
Ok, I managed to do this, took me 2 days to figure it out. Here is how I managed to do it:
Make a plugin folder.
In that plugin folder make 1x php file. so index.php
Ok so first thing we need to register plugin I did it like this, in your index.php paste
this code.
function activate_profile_plugin() {
add_option( 'Activated_Plugin', 'Plugin-Slug' );
/* activation code here */
}
register_activation_hook( __FILE__, 'activate_profile_plugin' );
Then we need a function when you register a plugin only once register profile pages.
function create_profile_page( $title, $slug, $post_type, $shortcode, $template = null ) {
//Check if the page with this name exists.
if(!get_page_by_title($title)) {
// if not :
$page_id = -1;
$page_id = wp_insert_post(
array(
'comment_status' => 'open',
'ping_status' => 'open',
'post_content' => $shortcode,
'post_author' => 1, // Administrator is creating the page
'post_title' => $title,
'post_name' => strtolower( $slug ),
'post_status' => 'publish',
'post_type' => strtolower( $post_type )
)
);
// If a template is specified in the function arguments, let's apply it
if( null != $template ) {
update_post_meta( get_the_ID(), '_wp_page_template', $template );
} // end if
return $page_id;
}
}
Ok so we created function which programatically register pages. It has 5 paramethers.
is Title
Slug
Post type
Shortcode.
Template
For the shortcode template you need to make a shortcode with the complete page output
and add it as a parameter to this function, so for registration page it will be a shortcode with the registration forms etc.
For example :
function registration_shortcode(){
echo 'Wellcome to Registration page';
}
add_shortcode('registration_output', 'registration_shortcode');
Next thing we need to call it once only when plugin loads.
so we do this :
function load_plugin() {
if ( is_admin() && get_option( 'Activated_Plugin' ) == 'Plugin-Slug' ) {
delete_option( 'Activated_Plugin' );
/* do stuff once right after activation */
// example: add_action( 'init', 'my_init_function' );
create_profile_page('Registration', 'registration', 'page', '[registration_output]');
create_profile_page('Profile', 'profile', 'page', '[profile_shortcode]');
create_profile_page('Profil Edit', 'profile-edit', 'page', '[edit_shortcode]');
}
}
add_action( 'admin_init', 'load_plugin' );
Ok so this will execute only once when plugin loads and it will create 3 Pages, which are Profile, Registration and Profile Edit.
And that's it, you have your front-end user profile blank pages, and you can write page output in shortcodes ,create more pages, put any forms or elements you like and create decent profile (which doesn't have any stuff you don't need in it like plugins. )
Hope this helps, it was painful for me to figure this out. Cheers!

Update all the posts at once nothing want to add or delete only update so that post slug get automatically generated..

I have changed the default permalink of my website to Custom permalink (/%post-name%)
Now all posts require slug values .
which is not present currently.A slug is automatically added when a post is published or updated if slug screen option is unable ,but in my case now i have unable the slug option for all post earlier this was disabled and Now I want to update each post nothing want to add or delete just want to update all posts .
Currently number of posts in database is 200000,
Please suggest any efficient query or any method so that my task can be accomplished.
Thanks,
Monika
Try this plugin, it should do the job:
http://www.jerrytravis.com/598/wordpress-plugin-to-generate-post-slugs
Also this chunk of code can do the job, but you have to add some limits to avoid the script to crash due to the max limit of execution:
// get all posts
$posts = get_posts( array ( 'numberposts' => -1 ) );
foreach ( $posts as $post )
{
// check the slug and run an update if necessary
$new_slug = sanitize_title( $post->post_title );
wp_update_post(
array (
'ID' => $post->ID,
'post_name' => $new_slug
)
);
}
Credit for the code:
https://wordpress.stackexchange.com/questions/46086/regenerate-slugs-from-title-of-posts

Creating a post using PHP in one of the blogs in WP MU

I have a PHP program to create a site/blog in a networking enabled WordPress. After the site creation, I want to create a post using the same PHP program.
If I use wp_insert_post() function, it's creating the post in the main site/blog, not in the new site/blog I created. I also tried using switch_to_blog() before calling the wp_insert_post() but no luck.
I got the answer for this... The culprit is $blog_id, the moment I changed the variable name to $new_blog_id, it started working. Thanks
The following used as Must Use plugin does the job:
<?php
/* Plugin Name: New Post on Site Creation */
add_action( 'wpmu_new_blog', 'default_post_so_5334372', 10, 6 );
function default_post_so_5334372( $blog_id, $user_id, $domain, $path, $site_id, $meta )
{
switch_to_blog( $blog_id );
$my_post = array(
'post_title' => 'My post',
'post_content' => 'This is my post.',
'post_status' => 'publish',
'post_author' => 1
);
wp_insert_post( $my_post );
restore_current_blog();
}

Resources