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!
Related
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.
I am modifying an existing plugin and I want to add a new field to the form and then have that field be submitted along with the post. The post gets submitted to wp_posts. I have read on Google that to do this one simply needs to use update_post_meta. I am trying to insert data into a new column I made in PHPMyAdmin. I named the column post_amount. The field name is amount_field. Although I'm a beginner, something about "just use update_post_meta" that I've read seems too simple to be all that I need. But I might be wrong. Maybe I'm using it wrong?
Note - this whole attempt is due to me wanting to create a new column in the wp_posts table and add data to it with each posts. Is this even the correct way to do this? I see the words "meta_key" and "meta_value" and it makes me think that this will actually end up adding data in the wp_postmeta table....or is that the intended destination?
$question_array = array(
'post_title' => $fields['title'],
'post_author' => $user_id,
'post_content' => apply_filters('ap_form_contents_filter', $fields['description']),
'post_type' => 'question',
'post_status' => $status,
'comment_status' => 'open',
);
if(isset($fields['parent_id']))
$question_array['post_parent'] = (int)$fields['parent_id'];
$question_array = apply_filters('ap_pre_insert_question', $question_array );
$post_id = wp_insert_post($question_array);
$post_amount = $fields['amount_field']; //My code
update_post_meta($post_id, 'post_amount', $post_amount); //My code
I think you made a mistake updating your post_id.
In update_post_meta($post_id, 'post_amount', $post_amount);
you don't have ID of the post or the ID you want to update.
$post_id parameter is declared to Insert Post in your above code.
$post_id = wp_insert_post($question_array); So, update post query didn't find the ID.
You need ID to update post meta. It didn't find any id so it didn't update meta_key. SORRY FOR MY ENGLISH.
Try using add_post_meta instead of update_post_meta.
See the Reference
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');
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.
This will return the link to the attachment:
$link=wp_get_attachment_link($image->ID);
However, I can't find a way to get the LINK TO value from the ATTACHMENT DISPLAY SETTINGS of an image. See screenshot below.
As yoavmatchulsky wrote, this field is dynamically filed by ~wp-includes/js/media-views.js after you manually choose an image
but if you have an id of attachment, use wp_get_attachment_link( $id, $size);
as size use 'full'
full ref. in codex
Or, if you are trying to use a custom link, the method described here might help.
Basically, in your functions.php, you could add a code similar to this:
// Adds a custom url field to your attachment
function attachment_custom_url( $form_fields, $post ) {
$form_fields['video-url'] = array(
'label' => 'CustomURL',
'input' => 'text',
'value' => get_post_meta( $post->ID, 'custom_url', true ),
'helps' => 'Add custom URL, if applicable',
);
return $form_fields;
}
add_filter( 'attachment_fields_to_edit', 'attachment_custom_url', 10, 2 );
function attachment_custom_url_save( $post, $attachment ) {
if( isset( $attachment['custom-url'] ) )
update_post_meta( $post['ID'], 'custom_url', esc_url( $attachment['custom-url'] ) );
return $post;
}
add_filter( 'attachment_fields_to_save', 'attachment_custom_url_save', 10, 2 );
And then, you could call it in your php like so:
<?php
Custom Link;
?>
I know this is an old thread, but I solve this by getting the post meta of the attachment, which was easier to me.
In my installation, custom URL input is shown like this:
<input type="text" class="text" id="attachments-140443-foogallery_custom_url" name="attachments[140443][foogallery_custom_url]" value="https://mycustomurl.com">
So, I assumed that if the first brackets contains the post ID, the second one is a meta key to save this value on wp_postmeta table. And there it was, just starting with the underscore character so it would be a hidden meta data. Therefore, the easier way to get this value is like this:
get_post_meta( get_post_thumbnail_id( get_the_ID() ), '_foogallery_custom_url', true);
Of course you need to check if the post does have a thumbnail, but that's easy to adapt.