0

I've created the code so that on publish of the page, the custom field 'bookinfo' in the page is automatically generated but its not doing anything

Below is my code in in functions.php:-

<?php
if (is_page()){  

function generate_page_html( $page_id ) {

$title=get_the_title();
$link=get_the_permalink();
$booktxt='new book'.$title.'at'.$link.'grab quickly';    

update_page_meta( $page_id, 'bookinfo', $booktxt); 
}

add_action( 'save_page', 'generate_page_html' );
}
?>

P.S i am using advanced custom fields to create the custom field where the rule is to display the field group of posttype is page

1 Answer 1

0

There are two problems that I can see that cause your code to fail.

The first is that you're using update_page_meta, which does not exist. Use update_post_meta instead.

The second is that you're using the action save_page, which also does not exist. Use save_post, save_post is an action triggered whenever a post or page is created or updated, so it is appropriate to use in your case.

For an overview of actions that you can use, see Action Reference

4
  • i made the changes as u said but the fields don't get updated even then Commented Mar 22, 2015 at 17:57
  • the whole code is in functions.php ..i want it to be applied when i update or publish a page so the custom field of the page i.e 'bookinfo' has the $booktxt thats generated Commented Mar 23, 2015 at 2:31
  • i am using advanced custom fields to create the custom field where the rule is to display the field group of posttype is page Commented Mar 23, 2015 at 2:40
  • is_page() probably isn't evaluating to true then Commented Mar 23, 2015 at 16:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.