Wordpress Edit Post Hook to load custom data - php

I have an meta_box in the post creation screen. As soon a user click on a specific category it will be displayed. For saving the information in the meta_box and post title and post content I use the hook post_updated. All is going to save fine in the database -- post data in wp_posts and the data in the meta_box into a special created table.
But sometimes the user want to edit this post from this 'special 'category. In this case the meta_box with the inserted information should be loaded and be editable. I know that I need to fill in the fields manually, but I need to know which hook is called when the user presses the edit link. An idea was, check for the url and if a parameter edit is found call my edit routine. But it looks like a dirty thing for me.
The codex is not very helpful. I tried the following hooks:
edit post, publish post, save post, wp insert post
But they are called after I pressed the actualize button.
BR,
mybecks

You don't have to use any hook for this, you just need to use following lines where you are generating your meta_box html
global $post;
now you have access the $post object when you are editing
get_post_meta($post_id, $key, $single);
use above function to get the value of particular meta and set it to your html as required.
for reading more about get_post_meta, goto http://codex.wordpress.org/Function_Reference/get_post_meta

Related

How can I find action update post in Wordpress Admin?

Im kinda new with wordpress. I wonder where is the code excute the action Update when I hit the button "Update" in wP admin when I edit post ?
Because I want to modify some information before it update to DB.
You don't really need to know where the code is located, Wordpress provides hooks for almost every action we do.
So in your case, when a post is updated there are some hooks that are fired.
pre_post_update Fires immediately before an existing post is updated in the database.
2.transition_post_status Fires when a post is transitioned from one status to another.
The 1st one
do_action( 'pre_post_update', int $post_ID, array $data )
You can run the pre_post_update hook and do whatever you need to do on the post.
https://developer.wordpress.org/reference/hooks/pre_post_update/
https://developer.wordpress.org/reference/hooks/transition_post_status/
https://codex.wordpress.org/Plugin_API/Action_Reference/post_updated

How can I automatically link words in post meta data and the_content to post titles?

I'm trying to create a glossary for my WordPress site, using a custom post type 'glossary'.
My idea is to create posts for each term/word I want to explain (for example: 'blepharoplasty'), and use the content of the post to provide a description.
This works fine standalone with single post pages, and an archive to display all of them.
But I want to hook this into the rest of my site and automatically find any instances of my custom post titles in post meta data and the_content, and if found, to provide a link to the single post page.
I know a filter/hook is needed, but I have no idea how to write the conditions.
I think 121679 is somewhere along the lines, but I just don't know how to reference my custom post type, and how to search against the current post I'm using.
Can anyone help?
function bv_glossary_content_links($null, $object_id, $meta_key, $single ) {
// What conditions can go here?
}
add_filter('get_post_metadata', 'bv_glossary_content_links', 10, 4);

Wordpress: How to ping URL from custom field on publish

i have a custom field for source-links. The URL inserted in this custom field should get pinged once the post gets published. I know there is a dedicated trackback field but i want the URL from the custom field automatically added.
As far as i understand, $add_ping is exactly that.
http://codex.wordpress.org/Function_Reference/add_ping
My problem is: i don't know where to add it. I imagine if i'd write this into my theme where i display the source-link, the source gets pinged everytime the post gets visited.
So whats the proper way to add an url to get pinged on publish?
To be clear: it is not a service that i want to get pinged. More like if you'd insert a link to post B into post A's content. Once post A gets published, post B (or its blog) gets pinged. I want to insert the link into a custom field of post A, instead of its content area.
I think Felipe is right. You could try something like this:
function doCustomPing ($post_id) {
$uri_to_ping = get_post_meta($post_id, 'FIELDNAME', true);
add_ping($post_id, $uri_to_ping);
}
add_action ('publish_post', 'doCustomPing');
So every time a post if published (that includes if its edited) then a hook into publish_post will run the doCustomPing function. If you're putting this as part of a theme drop the above code in your functions.php file. Put the name if your custom field where it says FIELDNAME.

How to add values in wp_postmeta?

I am working in wordpress.I have to add some fields to the my post and I want the added information of my post to be added in serailized form in metavalue in wp_postmeta.How can I do this?
I have found a page post.php that seem to handle this.But I just cannot seem to find anything that would let me add the values of my field to the postmeta.
HELP
Updated:
I have also seen add_post_meta Does this add the value into the database or just the adds the key and the value?
You need to add custom fields while adding new post to add new values in wp_postmeta.
Click on add new post and search for custom fields.
You can use the get_post_meta method to get a specific field or get_post_custom() to get them all.
But in this case i guess you ought to use the_meta(); that will get you the current post meta. once you add a custom field inside the post/page and put this in the template.
Learn how to use it Here

wordpress - manually set the author of a post in php

I'm using a wordpress theme that allows users to create posts (they don't need to be logged in). The problem is that when the posts are displayed, <?php the_author(); ?> always shows as "admin". The post-creation form has a name field that I can get at in functions.php, but how can I manually change the author of the post to the name that was entered in the form field?
Without seeing the context, I can only offer a step in the right direction. Within a plugin or functions.php in your theme, you can call on the wp_insert_post_data filter to change the data that will be inserted as your post. For instance, if you wanted every post to be attributed to author with ID 5, you could do something like:
function my_change_author( $data , $postarr )
{
$data['post_author'] = 5;
return $data;
}
add_filter('wp_insert_post_data' , 'my_change_author' , '99', 2);
I've done something like this before where I have front end forms that I capture as posts and automatically assign to a predefined author that I call Web Monkey or something like that ;) Check out this page for more: http://codex.wordpress.org/Plugin_API/Filter_Reference/wp_insert_post_data. This filter is really powerful in that you can alter your data that will become your post in anyway before it hits the database.
Have you checked what the_author_meta() contains? WP has to attach a userID to each post, so chances are it's assigning user 1 to those posts regardless of what they're entering into the box. Without seeing the custom function or knowing what theme you're using to test, it'll be pretty difficult to troubleshoot.

Categories