i created a custom table in my wp table called wp_publish fields : title, url, postdateword
i want to insert post data when post is published
for sample
I publish a new post and the post contains url, title, details
insert 3 field on wp_publish automatic.
it is possible ? i am creating plugin.
You can use the save_post, it is an action triggered whenever a post or page is created or updated
add_action( 'save_post', 'save_data' );
function save_data($post_id ){
// check $post_id in table if not exists in table insert,
}
please see this link http://codex.wordpress.org/Plugin_API/Action_Reference/save_post
You can try it works when new post publish
function authorNotification($post_id)
{
$post = get_post($post_id);
$wpdb->show_errors();
global $wpdb;
$wpdb->insert( 'tablename', array( 'name' => "inserted_text"), array('%s'));
$wpdb->print_error();
$wpdb->hide_errors();
}
add_action('publish_post', 'authorNotification');
Related
I want to automatically set my post title according to custom meta fields for example my custom meta fields have a Name of user I want the title of the post to be the Name of User hope you can help me with this
You can use WordPress' "save_post" hook to change the post title according to post meta whenever a post is saved. Lets say you fave a post meta field called "name_of_user" and you want to change the post title accordingly
add_action('save_post', 'wpse_update_post_title');
function wpse_update_post_title($post_id) {
// If this is just a revision, don't update the title yet.
if ( wp_is_post_revision( $post_id ) ) {
return;
}
// unhook this function so it doesn't loop infinitely
remove_action('save_post', 'wpse_update_post_title');
$user_name = get_post_meta( $post_id, 'name_of_user', true );
// Check if the meta for given key exists and then update the title
if($user_name) {
$slug = str_replace(' ', '-', strtolower($user_name))
$post_update = array(
'ID' => $post_id,
'post_title' => $user_name,
'post_name' => $slug // This swill update the url slug of the post too`enter code here`
);
wp_update_post( $post_update );
}
// re-hook this function
add_action('save_post', 'wpse_update_post_title');
}
The above code should go into your theme's functions.php file
We have an in-house WordPress plugin that creates content using a custom-post-type. Essentially, the content created is, for all intents and purposes, identical to WordPress 'Pages'.
The idea is that we add these custom 'pages' - via our plugin - and then deploy/update them for whomever is using the plugin. We've used our plugin to create a series of these pages, but the question is how to port this custom content with the plugin.
Initially we were thinking that since our posts have a custom post-type, we can easily identify them in the DB. In fact, we use that to remove our custom content when uninstalling the plugin.
But how do we do the inverse? Ideally we'd like to update the plugin, and with it, any custom content that we've added or modified.
Originally we were thinking of just using MySQL scripts to add this custom post content, but after a bit of research, this doesn't seem like the right way to do it.
I know WordPress has a wp_insert_post() function, but I'm just not sure of how it all fits together.
Ideally, the answer would be an overview of the process for updating our custom content. For instance, should there be a function in our plugin that, upon installation, looks for a sql file and creates new posts from it?
Thanks!
If I understand this correctly and you are trying to keep content sync'ed across multiple installations via plugin updates you will need to create a unique ID for all of your content. When you do updates programatically you are updating by the post ID but each installation will have different post IDs. You could use a post meta field to give each post a unique ID specific to your plugin. You can then query the posts based on your unique meta field.
I would code the plugin so it checks to see if there is a post with your unique ID. If it is not found then you insert a new post. If it is found then you update the post.
Let's say you have created your first piece of content and it's unique id is going to be jgohil_1. Your plugin might use something like the following to check for that unique id and then update or insert depending on if it exists.
<?php
// our meta key/value pair for our unique id
$meta_key = 'jgohil_unique_id';
$meta_value = 'jgohil_1';
// set our new content
$new_content = 'The new content here';
$new_title = 'The new title here';
// check the database to see if we have created the post already by querying for our unique id
global $wpdb;
$sql = $wpdb->prepare (
"SELECT post_id
FROM $wpdb->postmeta
WHERE meta_key = %s
AND meta_value = %s",
$meta_key,
$meta_value
);
$post_id = $wpdb->get_var( $sql );
// if we got a post id then update else insert
if ( $post_id ) {
// set up the data for updating
$data = array(
'ID' => $post_id,
'post_content' => $new_content,
'post_title' => $new_title,
);
// update the post
$updated = wp_update_post( $data );
if ( is_wp_error( $updated ) ) {
// do some error handling here
}
} else {
$data = array(
'post_title' => $new_title,
'post_content' => $new_content
'post_type' => 'your_custom_post_type'
);
$post_id = wp_insert_post( $data );
// if the insert worked give the post the unique meta id
if ( ! is_wp_error( $post_id ) && $post_id !== 0 ) {
update_post_meta( $post_id, $meta_key, $meta_value );
} else {
// do some error handling here
}
}
If you need to handle multiple pieces of content at once you would want to use a loop and change the meta value each time.
Is there a way to edit values added into database when I hook to media uploading ( I use add_attachment hook ) ?
Basically, what I want to do is:
hook to media upload
edit/update a field's value (post_status) from wp_posts table based on $attachment_id
return
Function would like this:
add_filter('add_attachment', 'handle_upload' );
function handle_upload($attachment_id){
//get attachment ID from database: how? I believe that at this point the database is not populated with the new post (media)
// update the post_status field from wp_posts table with some value
//return
}
Is this possible ?
Thanks in advance!
I found the solution.
The solution is to use wp_update_post:
function update_db_field($attachment_ID){
$attachment_details = get_post( $attachment_ID );
wp_update_post( array(
'ID' => $attachment_details->ID ,
'post_type' => "attachment"
)
);
}
add_action("add_attachment", 'update_db_field');
I hope it helps someone else!
I am using WordPress's Advanced Custom Fields 5 plugin to create a front-end form. I would like to save the post title as one of the fields on my form. For example, one of my form fields is 'name' so I would like the post title to be 'John Smith'.
Looking at the ACF documentation it gives example code (copied below) where pre_save_post can be hooked into achieve this. Hoewver, I have included this function and the title still fails to save.
Any ideas what I am doing wrong?
Here is the code:
function my_pre_save_post( $post_id )
{
// check if this is to be a new post
if( $post_id != 'new' )
{
return $post_id
}
// Create a new post
$post = array(
'post_status' => 'draft' ,
'post_title' => $_POST['fields']['field_123'] ,
'post_type' => 'post' ,
);
// insert the post
$post_id = wp_insert_post( $post );
// return the new ID
return $post_id;
}
add_filter('acf/pre_save_post' , 'my_pre_save_post', 10, 1 );
I am using exactly the same method and it works fine.
Just a note that field_123 in your example is not the user defined field name in the wp back end but its actually the field name assigned in the meta_key value in the wp_postmeta table of you db.
Hi all, thanks for reading.
Environment :
Wordpress + Advanced Custom Fields plugin
Problem :
I have searched for hours now and I can't seem to find the correct syntax to do the following:
When posting a new post, get custom field value in order to automatically replace the title of the post by that value. Example: I create a post and set '10am' in my 'time' custom field. The title of the post is automatically replaced by '10am'.
Example:
So I'm adding a filter with the following :
add_filter('wp_insert_post_data', 'change_title') ;
function change_title($data)
{
$time = XXX ; // How should I get this custom field value ?
$new_title = 'Topic created at'.$time ;
$data['post_title'] = $time ;
return $data;
}
It must be very simple but I have tried every function available on both WP and the plugin's documentations. I would be very thankful if anyone passing by gave me the solution.
Thanks in advance !
Tweak to Riadh's accepted answer (would add as a comment but haven't got enough rep yet):
As documented in the WordPress Codex wp_update_post includes the save_post hook so calling wp_update_post() inside the save_post hook creates an infinite loop. To avoid this, unhook then rehook your function like so:
add_action('save_post', 'change_title');
function change_title($post_id) {
$time = get_field('time',$post_id);
$post_title = 'Topic created at '. $time;
// unhook this function so it doesn't loop infinitely
remove_action('save_post', 'change_title');
// update the post, which calls save_post again
wp_update_post(array('ID' => $post_id, 'post_title' => $post_title));
// re-hook this function
add_action('save_post', 'change_title');
}
You can actually access the global $_POST variable for your field value , but i guess you can do it in a cleaner way by using the save_post action to update your post's title, eg:
add_action('save_post', 'change_title');
function change_title($post_id) {
$time = get_field('time',$post_id);
$post_title = 'Topic created at '. $time;
// unhook this function so it doesn't loop infinitely
remove_action('save_post', 'change_title');
// update the post, which calls save_post again
wp_update_post(array('ID' => $post_id, 'post_title' => $post_title));
// re-hook this function
add_action('save_post', 'change_title');
}
assuming that your ACF fieldname is "time".
Edit: Updated the answer as per Mark Chitty's answer.
You may try this
add_filter( 'wp_insert_post_data', 'change_title', '99', 2 );
function change_title($data , $postarr){
$custom_field = 'custom_filed_name';
$post_id = $postarr['ID'];
$time = get_post_meta( $post_id, $custom_field, true );
// Now you have the value, do whatever you want
}
Advanced Custom Fields creates a 'field key' for each custom field that is created. I was able to refer to this key value when trying to access the custom fields. The field key value can be found by viewing page source when viewing the post type within the Wordpress admin section.
Look for data-field-key. You will see a value similar to data-field-key="field_5847b00820f13" in the page source. Use this value when accessing the value in the $postarr argument in the wp_insert_post_data filter. The custom fields will be in a nested array named fields within the $postarr argument.
Alternatively, the field key value can be located by navigating to the Advanced Custom Fields / Export option from within the admin section. Once you are on the export page for Advanced Custom Fields, select the export to PHP option and you will see the value in the resulting PHP code.
In the example below, I am concatenating two Advanced Custom Fields and updating the post_title in the $data array returned from the function.
The result is that the post_title value will be saved to the database via the built in Wordpress save post logic.
add_filter('wp_insert_post_data', 'slb_set_title', '99', 2);
function slb_set_title ($data, $postarr){
if($data['post_type']==='slb_subscriber'){
$data['post_title'] = $postarr['fields']['field_5847b00820f13'] .' '.
$postarr['fields']['field_5847b03f20f14'];
}
return $data;
}