I am getting this error:
Fatal error: Call to undefined function update_post_meta()
My code is as below. Can any one help me out?
<?php if(isset($_POST['submit']))
{
$metavalue = $_POST['usertext'];
$postid = $_POST['postid'];
$metakey = 'my_key';
update_post_meta( $postid, $metakey, $metavalue );
} ?>
<div class="wrap">
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="postid" value="<?php echo $_GET['value'] ?>" >
<input type="hidden" name="123" value="123" >
<input type="text" name="usertext">
<br/>
<input type="submit" name="submit" value="Update">
</form>
</div>
<?php
I'm guessing this is within a theme of some sort. You need to assign a post to the global $post variable.
Try
//your stuff until here
$metakey = 'my_key';
$current_post = get_post($_POST['postid']);
if($current_post){
global $post = $current_post
setup_postdata($post);
update_post_meta( //continue
//more continueing
} else {
//throw some error
}
get_post gets the post based on some sort of variable, or if you give it nothing it returns a false.
global $post is the WP global variable to hold your current post. Be careful using this as it might mess up code after this if you're assuming the loop continues automatically, or if you need the loop.
setup_postdata Sets up global post data. Helps to format custom query results for using Template tags.
Hopefully this will help you out. Next time you ask a question also have a look at this and this.
Related
I am trying to create a button "Approved" to change the post category from it's current one to the "approved" category. I don't mind if it reloads the page or not. I would also like to redirect the page afterward to the next post in the original category.
I have found some questions on this already but am ultimately lost on how to get this all together and working.
<?php add_shortcode('approved_button', 'brist_approved_button_function');
function brist_approved_button_function() {
ob_start(); ?>
<form method="post" action="approved.php">
<input type="submit" value="Approved" name="submit"> <!-- assign a name for the button -->
</form>
<?php
wp_set_object_terms( $post_id, intval( $_POST['approved'] ), 'category', false );
$output = ob_get_clean();
return $output;
}?>
I figured it out. Though it was a head-scratcher for me. If anyone else if having the same issue, note the code below. You have to create the select and then have the 'selected' option on the category you want the post to change to. Then, in the CSS, hide the select input, only leaving the button.
<?php add_shortcode('approved_button', 'approved_button_function');?>
<?php function approved_button_function() { ob_start();?>
<div class="approval">
<form action="" id="update-post" method="post">
<?php wp_dropdown_categories( "selected='categoryId'&exclude=21&class=approval-select&show_count=1&hierarchical=1&orderby=name&order=ASC&&hide_empty=0&show_option_all=Choose An Option" ); ?>
<input class="approval-button" type="submit" name="submit" value="Approve" />
</form>
</div>
<?php if ( array_key_exists('cat', $_POST )) {
global $post;
$post_id = $post->ID;
wp_set_object_terms( $post_id, intval( $_POST['cat'] ), 'category', false );
} ?>
I've created a variable within a function it looks like this:
function my_plugin_options() {
//must check that the user has the required capability
if (!current_user_can('manage_options'))
{
wp_die( __('You do not have sufficient permissions to access this page.') );
}
// variables for the field and option names
$opt_name = 'mt_favorite_color';
$hidden_field_name = 'mt_submit_hidden';
$data_field_name = 'mt_favorite_color';
// Read in existing option value from database
$opt_val = get_option( $opt_name );
doingthistest($opt_val);
// See if the user has posted us some information
// If they did, this hidden field will be set to 'Y'
if( isset($_POST[ $hidden_field_name ]) && $_POST[ $hidden_field_name ] == 'Y' ) {
// Read their posted value
$opt_val = $_POST[ $data_field_name ];
// Save the posted value in the database
update_option( $opt_name, $opt_val );
// Put a "settings saved" message on the screen
?>
<div class="updated"><p><strong><?php _e('settings saved.', 'help-menu-settings' ); ?></strong></p></div>
<?php
}
// Now display the settings editing screen
echo '<div class="wrap">';
// header
echo "<h2>" . __( 'Help block details', 'help-menu-settings' ) . "</h2>";
// settings form
?>
<form name="form1" method="post" action="">
<input type="hidden" name="<?php echo $hidden_field_name; ?>" value="Y">
<p><?php _e("Whatever:", 'menu-test' ); ?>
<input type="text" name="<?php echo $data_field_name; ?>" value="<?php echo $opt_val; ?>" size="20">
</p><hr />
<p class="submit">
<input type="submit" name="Submit" class="button-primary" value="<?php esc_attr_e('Save Changes') ?>" />
</p>
</form>
<span><?php echo $opt_val ?></span>
</div>
<?php
}
Now I can echo out the $opt_val variable within the scope of that function but i'm struggling to access it outside.
You'll see where I set the variable $opt_val = get_option( $opt_name ); below it I pass it to a function doingthistest($opt_val); then I create an action below so I can call it in another page (WordPress method).
So my action below looks like:
add_action('testingthis', 'doingthistest');
function doingthistest(t) {
var_dump(t);
}
for some reason, the variable isn't getting passed to my action. Am I misunderstanding something?
I call it in another page like this:
<span>info is there: <?php do_action( 'testingthis' ) ?></span>
if you want something to be available in a different page (therefore presumably during a different HTTP request?) then you'd have to put it into a session variable or other persistent storage (e.g. file, database) and the retrieve it from there in your other page. HTTP is inherently stateless and the server won't remember the value of your variables from one request to the next unless you use one of the above mechanisms to store them.
Calling doingthistest($opt_val); within the context of the code you posted will dump the variable onto that page. But then if you call that same method from another page entirely - which must by definition be in a different request - it doesn't automatically remember what you the value of it was last time. It's a separate request with a separate context.
I don't understand the Wordpress stuff precisely, but I suspect your action would be better looking something like this:
add_action('testingthis', 'doingthistest');
function dosomething($o) {
var_dump(get_option( $opt_name ));
}
But obviously you'd have to set the value of $opt_name somehow. In your sample code it's hard-coded but I don't know if that's really how you're setting it in the finished solution.
I'm developing a WordPress plugin which is responsible for getting data from an external HTML form and then inserts it in the database. My form code is:
<html>
<body>
<form action = "http://wp.istic.online/wp-content/plugins/news-apps/index.php" method = "POST">
Name: <input type = "text" name = "name" />
<input type = "submit" />
</form>
</body>
</html>
My plugin code is:
<?php
register($_POST['name']);
register($_POST['name']);
function register($name) {
global $wpdb;
$wpdb->insert(
'gcm_tokens',
array(
'token' => $name
),
array(
'%s'
)
);
}
The data is not inserted however when I try to echo the $name variable it works perfectly. But when I try to echo it after the insert function it's not working.
Any help please?
Use wp_nonce_field() like below
<form>
/*
Your code
*/
<input type="hidden" name="action" value="new_post" />
<?php wp_nonce_field( 'new-post' ); ?>
</form>
First, make sure you have correct db prefix, correct table name and you can get $POST variable correctly.
I tried your code by call register('Test'); and It works.
I have a little form that updates a custom user meta I made named "Doel":
<?php $user_ID = get_current_user_id(); $user_data = get_user_meta($user_ID); ?>
<form method="POST" action="" id="doel_edit">
<input type="text" value="<?php if(isset($user_data['doel'][0])): echo $user_data['doel'][0]; endif; ?>" name="doel">
<button value="done" type="submit" form="doel_edit">Opslaan</button>
</form>
When I submit the form I get the standard update_user_meta() function to run
<?php if(isset($_POST['doel'])) {
update_user_meta($user_ID, 'doel', $_POST['doel']);
} ?>
No this all works fine, but when the page loads after submitting the form it echo's the old value from before update_user_meta() ran. I have to reload the page by hand to get my new value.
Why doesn't the value update directly? How can I fix that?
I think running functions like these in the wp-admin works fine.
I actually found the answer with help from somewhere else.
Because I ran get_user_meta() before update_user_meta() the values didn't update. Once I placed update_user_meta() after get_user_meta() it worked:
<?php $user_ID = get_current_user_id(); ?>
<?php if(isset($_POST['doel'])) {
update_user_meta($user_ID, 'doel', $_POST['doel']);
} ?>
<?php $user_data = get_user_meta($user_ID); ?>
<form method="POST" action="" id="doel_edit">
<input type="text" value="<?php if(isset($user_data['doel'][0])): echo $user_data['doel'][0]; endif; ?>" name="doel">
<button value="done" type="submit" form="doel_edit">Opslaan</button
</form>
I want my bloggers to be able to delete comments via the front end instead of the standard way via the WP dashboard. I wrote a function custom_delete_post_comment() which deletes a comment with a given ID.
function custom_delete_post_comment() {
$comment_id = comment_ID();
wp_delete_comment( $comment_id, true )
}
As you can see, my function uses WordPress' wp_delete_comment() function.
I plan on having a button next to each comment which when clicked will run the delete function I wrote hence removing the comment.
I have come up with a solution using the $_POST approach. My question is how do I modify my code so that the page reloads to reflect the fact that the comment has been deleted?
<?php if( 'POST' == $_SERVER['REQUEST_METHOD'] ) {
set_query_var( 'commentid1', $_POST['commentid'] );
wp_delete_comment( get_query_var( 'commentid1'), true );
};
?>
<form class="delete-comment" action="" method="post">
<input type="hidden" name="commentid" value="<?php comment_ID() ?>" />
<input type="submit" value="Delete" title="Delete" class="btn" />
</form>