My question is on save($postID) function. Here there is a parameter $postID which is using in the function for saving as id. I see that this $postID has a null value. How does actual post id work for $postID?
This is the simple meta-box code
/* simple meta box */
/* creating field */
add_action('admin_menu', 'my_post_options_box');
function my_post_options_box() {
if ( function_exists('add_meta_box') ) {
add_meta_box('post_header', 'Asif, save me!', 'testfield', 'post', 'normal', 'low');
}
}
function testfield(){
global $post;
?>
<input type="text" name="Asif" id="Asif" value="<?php echo get_post_meta($post->ID, 'Sumon', true); ?>">
<?php
}
/* end of creating field */
/* storing field after pressing save button */
add_action('save_post', 'save');
function save($postID){
if (!defined('DOING_AUTOSAVE') && !DOING_AUTOSAVE) {
return $postID;
}
else
{
if($parent_id = wp_is_post_revision($postID))
{
$postID = $parent_id;
}
if ($_POST['Asif'])
{
update_custom_meta($postID, $_POST['Asif'], 'Sumon');
}
}
}
// saving in postmeta table
function update_custom_meta($postID, $newvalue, $field_name){
if(!get_post_meta($postID, $field_name)){
// create field
add_post_meta($postID, $field_name, $newvalue);
}
else{
//update field
update_post_meta($postID, $field_name, $newvalue);
}
}
You've used the wrong Action Hook.
Instead of using add_action('admin_menu', 'my_post_options_box');
Use add_action('add_meta_boxes', 'my_post_options_box');
add_action('add_meta_boxes', 'my_post_options_box');
function my_post_options_box() {
if ( function_exists('add_meta_box') ) {
add_meta_box('post_header', 'Asif, save me!', 'testfield', 'post', 'normal', 'low');
}
}
You can look in to http://codex.wordpress.org/Function_Reference/add_meta_box for detailed overview.
Some SO Question/Answers you can look into.
Add multiple dates to custom post type in Wordpress
how to add a meta box to wordpress pages
While Saving Post
Action save_post automatically passes Post ID to callback function. Which you can use inside your callback function.
Some already answered quesitons
https://wordpress.stackexchange.com/questions/41912/perform-an-action-when-post-is-updated-published
Reference
http://codex.wordpress.org/Plugin_API/Action_Reference/save_post
Related
I have a custom post type named 'project' and using a template for it "single-project.php"
which is working fine.
Now I want to have a some differentiation, what I need to assign the template "single-project.php" if a custom value in 'project' post type is mobile and "project-website.php" template if that custom meta value is equal to website.
means, need code to change the templates dynamically on the basis of custom meta value.
Thanks
The easiest way is to just call the "project-website.php" file from the "single-project.php" file, if the custom meta value is equal to "website".
Edit your "single-project.php" file:
<?php
$custom_field_value = get_post_meta($post_id,'your-custom-meta-key',true);
if($custom_field_value == 'website'){
echo get_template_part('single-project');
} else {
*** ENTER YOUR CURRENT CODE FOR SINGLE-PROJECT.PHP HERE ***
}
?>
#Jeremy good point, I already know this but I did it this way , Thanks,
function get_custom_post_type_template($single_template)
{
global $post;
$object = get_queried_object();
$id = $object->ID;
$post_type = $object->post_type;
$is_mobile = get_field('is_mobile_app', $id);
if ($is_mobile !== true && $post_type == 'project') {
return $single_template = dirname(__FILE__) . '/single-project-website.php';
} else {
return $single_template;
}
// return $single_template;
}```
I am building a plugin that provides author names as checkboxes on the admin side. The names that have been checked should be displayed on the front-end side in Wordpress.
I have written the code for the admin side, i.e. my code provides checkboxes and its values are saved.
How should the checked values be displayed on the front end?
<?php
add_action('add_meta_boxes', 'add_custom_boxx');
function add_custom_boxx($post) {
add_meta_box(
'Meta Box', // ID, should be a string.
'Featured People', // Meta Box Title.
'people_meta_boxx', // Your call back function, this is where your form field will go.
'post', // The post type you want this to show up on, can be post, page, or custom post type.
'side', // The placement of your meta box, can be normal or side.
'core' // The priority in which this will be displayed.
);
}
function people_meta_boxx($post) {
wp_nonce_field('my_awesome_nonce', 'awesome_nonce');
$checkboxMeta = get_post_meta($post->ID);
$html = "";
$blogusers = get_users('blog_id=1&orderby=nicename&role=author');
// Array of WP_User objects.
foreach ($blogusers as $user) {
?>
<input type="checkbox"
name="<?php echo $user->display_name;?>"
id="<?php echo $user->display_name;?>"
<?php if (isset($checkboxMeta[$user->display_name])) {
checked($checkboxMeta[$user->display_name][0], 'yes'); } ?> />
<?php echo $user->display_name;?><br />
<?php
} // end foreach
?>
<?php } // end function people_meta_boxx
add_action('save_post', 'save_people_checkboxesx');
function save_people_checkboxesx($post_id) {
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return;
if ((isset($_POST['my_awesome_nonce'])) && (! wp_verify_nonce($_POST['my_awesome_nonce'], plugin_basename(__FILE__))))
return;
if ((isset($_POST['post_type'])) && ('page' == $_POST['post_type'])) {
if (! current_user_can('edit_page', $post_id)) {
return;
}
} else {
if (! current_user_can('edit_post', $post_id)) {
return;
}
}
$blogusers = get_users('blog_id=1&orderby=nicename&role=author');
// Array of WP_User objects.
foreach ($blogusers as $user) {
//saves bob's value
if (isset($_POST[$user->display_name])) {
update_post_meta($post_id, $user->display_name, 'yes');
} else {
update_post_meta($post_id, $user->display_name, 'no');
}
}
}
function display_contributors($content) {
//Code to be inserted here.
return $content;
}
add_filter('the_content','display_contributors');
?>
I'm trying to modify Wordpress "press this" http://codex.wordpress.org/Press_This to post with the post type that my theme created.
By default Post this open the link http://www.xxxx.com/wp-admin/post-new.php
And I want it to open http://www.xxxx.com/wp-admin/post-new.php?post_type=recipe
Have tried the following code in Functions.php but nothing happens
function press_this_ptype($link) {
$post_type = 'recipe';
$link = str_replace('post-new.php', "post-new.php?post_type=$post_type", $link);
return $link;
}
add_filter('shortcut_link', 'press_this_ptype', 11);
Studying the file, I think the best entry point is the function wp_update_post() that has a filter we can use:
add_action( 'load-press-this.php', function() # Add filter only in Press This
{
add_filter( 'wp_insert_post_data', function( $data, $postarr )
{
$data['post_type'] = 'portfolio'; # <-- adjust CPT
return $data;
}, 10, 2 );
});
After that, we'll notice the need to hide some stuff from the Press This screen (post formats, categories, tags):
add_action( 'admin_head-press-this.php', function()
{
?>
<style type='text/css'>
#categorydiv, #tagsdiv-post_tag, /* Category and tag meta boxes */
#submitdiv div.inside p:nth-of-type(2) /* Post formats */
{
display:none;
}
</style>
<?php
});
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.
The code below is working nearly flawlessly, however my value for page title on one of my pages keeps coming up empty after a few page refreshes... It sticks for awhile, then it appears to reset to empty. I'm thinking I must have a conflict in the code below, but I can't quite figure it.
I'm allowing the user to set a custom page title for posts as well as pages via a custom "post/page title input field). Can anyone see an obvious issue here that might be resetting the page title to blank?
// ===================
// = POST OPTION BOX =
// ===================
add_action('admin_menu', 'my_post_options_box');
function my_post_options_box() {
if ( function_exists('add_meta_box') ) {
//add_meta_box( $id, $title, $callback, $page, $context, $priority );
add_meta_box('post_header', 'Custom Post Header Code (optional)', 'custom_post_images', 'post', 'normal', 'low');
add_meta_box('post_title', 'Custom Post Title', 'custom_post_title', 'post', 'normal', 'high');
add_meta_box('post_title_page', 'Custom Post Title', 'custom_post_title', 'page', 'normal', 'high');
add_meta_box('postexcerpt', __('Excerpt'), 'post_excerpt_meta_box', 'page', 'normal', 'core');
add_meta_box('categorydiv', __('Page Options'), 'post_categories_meta_box', 'page', 'side', 'core');
}
}
//Adds the custom images box
function custom_post_images() {
global $post;
?>
<div class="inside">
<textarea style="height:70px; width:100%;margin-left:-5px;" name="customHeader" id="customHeader"><?php echo get_post_meta($post->ID, 'customHeader', true); ?></textarea>
<p>Enter your custom html code here for the post page header/image area. Whatever you enter here will override the default post header or image listing <b>for this post only</b>. You can enter image references like so <img src='wp-content/uploads/product1.jpg' />. To show default images, just leave this field empty</p>
</div>
<?php
}
//Adds the custom post title box
function custom_post_title() {
global $post;
?>
<div class="inside">
<p><input style="height:25px;width:100%;margin-left:-10px;" type="text" name="customTitle" id="customTitle" value="<?php echo get_post_meta($post->ID, 'customTitle', true); ?>"></p>
<p>Enter your custom post/page title here and it will be used for the html <title> for this post page and the Google link text used for this page.</p>
</div>
<?php
}
add_action('save_post', 'custom_add_save');
function custom_add_save($postID){
if (!defined('DOING_AUTOSAVE') && !DOING_AUTOSAVE) {
return $postID;
}
else
{
// called after a post or page is saved and not on autosave
if($parent_id = wp_is_post_revision($postID))
{
$postID = $parent_id;
}
if ($_POST['customHeader'])
{
update_custom_meta($postID, $_POST['customHeader'], 'customHeader');
}
else
{
update_custom_meta($postID, '', 'customHeader');
}
if ($_POST['customTitle'])
{
update_custom_meta($postID, $_POST['customTitle'], 'customTitle');
}
else
{
update_custom_meta($postID, '', 'customTitle');
}
}
}
function update_custom_meta($postID, $newvalue, $field_name) {
// To create new meta
if(!get_post_meta($postID, $field_name)){
add_post_meta($postID, $field_name, $newvalue);
}else{
// or to update existing meta
update_post_meta($postID, $field_name, $newvalue);
}
}
?>
Wordpress's auto save system may well be your problem, as I think custom fields are not passed along for auto saves (so your customHeader and customTitle post variables will be empty during an auto save).
In your save function you should check if the DOING_AUTOSAVE constant is set (this seems to be preferable to checking the post action) and return if so. Something like this:
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
See this ticket for more info: http://core.trac.wordpress.org/ticket/10744
The WordPress Codex has got an function reference for add_meta_box(), with a great example.
And yes, it uses
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return $post_id;
and I think you've implemented it the right way.
FYI, the solution posted here http://wordpress.org/support/topic/custom-post-type-information-disappearing worked for me and I think is much more elegant.