Wordpress in foreach loop updating meta values - php

I have done foreach loop to get custom post type to frontend. And i have one custom field called 'order_staatus'. When i look in front end my list in loop, i want to add one button what would change this certain post 'order_staatus' to different value.. my code..
<? foreach ( $postslist as $post ) :
setup_postdata( $post );
?>
<div class="profile_order_row">
<?php the_author(); ?> <?php the_title(); ?>
<?php
Global $post;
if ( isset( $_POST['submit'] ) )
{
if( ! isset( $post ) ) {
echo 'Post not set';
die();
}
else if( ! isset( $_POST['staatus'] ) && ! empty( $_POST['staatus'] ) ){
echo 'Error';
die();
}
$postid = $_POST['post_id'];
update_post_meta($postid,'order_staatus','1');
}
$staatus = get_post_meta($post->ID, 'order_staatus', true);
echo print_r($staatus);
?>
<form method="post" action="">
<input type="hidden" name="post_id" value="'.$post->ID.'" />
<input type='text' name='staatus' value='<? echo $staatus ?>' />
<input type='submit' value='save' />
</form>
</div>
<?php endforeach; wp_reset_postdata(); ?>

I don't think the issue is with the function update_post_meta. I think the more likely issue is that $_POST['submit'] is not set. When submitting a form, the value of the submit button is not sent. The value attribute is simply for assigning the text of the button.
I would rewrite the IF block of code like this:
if ( isset( $_POST ) )
{
if( ! isset( $_POST['staatus'] ) && ! empty( $_POST['staatus'] ) ){
echo 'Error';
die();
}
$postid = sanitize_text_field($_POST['post_id']);
update_post_meta($postid,'order_staatus','1');
}
Note, that I removed the isset($post) check because you are running this code inside of foreach ( $postslist as $post ) which defines $post so it will always be set within that loop.
I also added the function sanitize_text_field() to sanitize the post_id. This is an important security measure to avoid SQL injection. All user input, including $_POST, can contain dangerous data and needs to be sanitized before use.
Hope this helps!

Related

calling a php function in form - wordpress

im trying to call a php function in submiting a form.
here my full php file: ( am i write correct? )
<?php
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
?>
<div class="manual_sending_form" id="manual_sending_form">
<div class="inside">
<form class="initial-form" id="manual_sending" method="post" name="post"
action="<?php echo admin_url( 'admin.php?page=my-page' ) ?>">
<p>
<label for="resive_number">My Field</label><br>
<input type="text" name="resive_number" id="resive_number"
value="<?php echo isset( $_POST['sms_numbers'] ) ? sanitize_text_field( $_POST['sms_numbers'] ) : '' ?>"
</p>
<p>
<input type="submit" class="button button-primary" name="send_sms_button"
value="Send">
</p>
</form>
</div>
</div>
<?php
if ( isset( $_POST['send_sms_button'] ) ) {
try
{
$phone = $_POST['sms_numbers'];
// do some code hwre
?>
did i correct on below line? i just want to input something there ( i need this value on function.
value="<?php echo isset( $_POST['sms_numbers'] ) ? sanitize_text_field( $_POST['sms_numbers'] ) : '' ?>"
or this part is correct ?
( $phone = $_POST['sms_numbers']; )
i dont know why i get below error, ////
Notice: Undefined index: sms_numbers in
Try to replace your code into this
<input type="text" name="sms_numbers" // change input name to sms_numbers
and set this line at the top of your page
ini_set('display_errors',0);

Wordpress add_meta_box does not do anything

I can't seem to get the Wordpress add_meta_box to work.
It is adding the boxes in the edit page, but it isn't reflected in the input boxes anywhere.
How can I get this to work? Is there anything I'm doing wrong? I followed this tutorial.
// Add support for header images
if(function_exists('add_theme_support')){
add_theme_support('post-thumbnails'); // Adds this for both pages and posts, For posts/page only, see https://codex.wordpress.org/Function_Reference/add_theme_support
}
// Callback for adding metaboxes
function actionblock_add_meta_box(){
$screens = array( 'post', 'page' );
foreach ( $screens as $screen ) {
add_meta_box('action_block', 'Action Block', 'actionblock_meta_box_callback', $screen, 'normal'); // Add the action block to both post and page screens
}
}
// Add meta boxes for action section on each post/page
add_action('add_meta_boxes', 'actionblock_add_meta_box');
function actionblock_meta_box_callback($post){
// Security check
wp_nonce_field('actionblock_save_meta_box_data', 'actionblock_meta_box_nonce');
// Get values of the action block fields
$action_prompt_text = get_post_meta($post->ID, 'actionblock_meta_prompt_text', true);
$action_button_text = get_post_meta($post->ID, 'actionblock_meta_button_text', true);
$action_button_link = get_post_meta($post->ID, 'actionblock_meta_button_link', true);
// Display and populate fields from the database if it exists
?>
<p>
<label for="action_prompt_text">Action Prompt</label><br>
<input class="widefat" type="text" name="action_prompt_text" id="action_prompt_text" placeholder="This should be short, to the point and less salesy." value="<?php echo esc_attr($action_prompt_text); ?>">
</p>
<p>
<label for="action_button_text">Action Button Text</label><br>
<input class="widefat" type="text" name="action_button_text" id="action_button_text" placeholder="This should prompt the visitor to take an action." value="<?php echo esc_attr($action_button_text); ?>">
</p>
<p>
<label for="action_button_link">Action Button Link</label><br>
<input class="widefat" type="url" name="action_button_link" id="action_button_link" placeholder="Copy and paste the link from the intended page." value="<?php echo esc_attr($action_button_link); ?>">
</p>
<?php
// Callback for saving metaboxes
function actionblock_save_meta_box_data($post_id) {
// Security checks
if(!isset($_POST['actionblock_meta_box_nonce'])) return;
if(!wp_verify_nonce($_POST['actionblock_meta_box_nonce'], 'actionblock_save_meta_box_data')) return;
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) 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;
}
}
// Do the save/update
if(isset($_POST['action_prompt_text'])){
update_post_meta($post_id, 'actionblock_meta_prompt_text', sanitize_text_field($_POST['action_prompt_text']));
}
if(isset($_POST['action_button_text'])){
update_post_meta($post_id, 'actionblock_meta_button_text', sanitize_text_field($_POST['action_button_text']));
}
if(isset($_POST['action_prompt_link'])){
update_post_meta($post_id, 'actionblock_meta_button_link', esc_url($_POST['action_prompt_link']));
}
}
// Save action block details when the post is saved/updated
add_action('save_post', 'actionblock_save_meta_box_data');
Try Below code :
// Add support for header images
if(function_exists('add_theme_support')){
add_theme_support('post-thumbnails'); // Adds this for both pages and posts, For posts/page only, see https://codex.wordpress.org/Function_Reference/add_theme_support
}
// Callback for adding metaboxes
function actionblock_add_meta_box(){
$screens = array( 'post', 'page' );
foreach ( $screens as $screen ) {
add_meta_box('action_block', 'Action Block', 'actionblock_meta_box_callback', $screen, 'normal'); // Add the action block to both post and page screens
}
}
// Add meta boxes for action section on each post/page
add_action('add_meta_boxes', 'actionblock_add_meta_box');
function actionblock_meta_box_callback($post){
// Security check
wp_nonce_field('actionblock_save_meta_box_data', 'actionblock_meta_box_nonce');
// Get values of the action block fields
$action_prompt_text = get_post_meta($post->ID, 'actionblock_meta_prompt_text', true);
$action_button_text = get_post_meta($post->ID, 'actionblock_meta_button_text', true);
$action_button_link = get_post_meta($post->ID, 'actionblock_meta_button_link', true);
// Display and populate fields from the database if it exists
?>
<p>
<label for="action_prompt_text">Action Prompt</label><br>
<input class="widefat" type="text" name="action_prompt_text" id="action_prompt_text" placeholder="This should be short, to the point and less salesy." value="<?php echo esc_attr($action_prompt_text); ?>">
</p>
<p>
<label for="action_button_text">Action Button Text</label><br>
<input class="widefat" type="text" name="action_button_text" id="action_button_text" placeholder="This should prompt the visitor to take an action." value="<?php echo esc_attr($action_button_text); ?>">
</p>
<p>
<label for="action_button_link">Action Button Link</label><br>
<input class="widefat" type="url" name="action_button_link" id="action_button_link" placeholder="Copy and paste the link from the intended page." value="<?php echo esc_attr($action_button_link); ?>">
</p>
<?php
}
// Callback for saving metaboxes
function actionblock_save_meta_box_data($post_id) {
// Security checks
if(!isset($_POST['actionblock_meta_box_nonce'])) return;
if(!wp_verify_nonce($_POST['actionblock_meta_box_nonce'], 'actionblock_save_meta_box_data')) return;
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) 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;
}
}
// Do the save/update
if(isset($_POST['action_prompt_text'])){
update_post_meta($post_id, 'actionblock_meta_prompt_text', sanitize_text_field($_POST['action_prompt_text']));
}
if(isset($_POST['action_button_text'])){
update_post_meta($post_id, 'actionblock_meta_button_text', sanitize_text_field($_POST['action_button_text']));
}
if(isset($_POST['action_button_link'])){
update_post_meta($post_id, 'actionblock_meta_button_link', esc_url($_POST['action_button_link']));
}
}
// Save action block details when the post is saved/updated
add_action('save_post', 'actionblock_save_meta_box_data');

ACF update_field associated with a User

I've created some custom fields on the user profile page in WordPress.
I've managed to build a front end editor for the user to change some e-mail preferences:
<?php if( isset($_POST['preferences']) ) :
update_field('planner-reminders', $_POST['planner-reminders'], 'user_'.$user_id.'');
update_field('event-suggestions', $_POST['event-suggestions'], 'user_'.$user_id.'');
update_field('woa-updates', $_POST['woa-updates'], 'user_'.$user_id.'');
echo '<p class="success">E-Mail Prefereneces Updated<p>';
endif; ?>
<form action="<?php the_permalink(); ?>" method="POST" class="user-email-settings">
<!-- planner reminders -->
<?php
$field = get_field('planner-reminders', 'user_'.$user_id.'');
if( $field == true ) {
$field = '1';
$checked = true;
}
?>
<input type="checkbox" id="planner-reminders" name="planner-reminders" class="pref"
value="<?php echo $field; ?>" <?php if($checked) :?> checked <?php endif;?> />
<label for="planner-reminders">I don't want to revieve reminders about events I've added to my planner.</label>
<?php $checked = false; ?>
<!-- event suggestions from WOA -->
<?php
$field = get_field('event-suggestions', 'user_'.$user_id.'');
if( $field == true ) {
$field = '1';
$checked = true;
}
?>
<input type="checkbox" id="event-suggestions" name="event-suggestions" class="pref"
value="<?php echo $field; ?>" <?php if($checked) :?> checked <?php endif;?> />
<label for="event-suggestions">I don't want to recieve suggestions about events I may be interested in.</label>
<?php $checked = false; ?>
<!-- updates from WOA -->
<?php
$field = get_field('woa-updates', 'user_'.$user_id.'');
if( $field == true ) {
$field = '1';
$checked = true;
}
?>
<input type="checkbox" id="woa-updates" name="woa-updates" class="pref"
value="<?php echo $field; ?>" <?php if($checked) :?> checked <?php endif;?> />
<label for="woa-updates">I don't want to recieve e-mail updates from What's On Advisor.</label>
<?php $checked = false; ?>
<input type="submit" value="Save Preferences" name="preferences" id="preferences" />
</form>
Now, this actually seems to work. If I update a checkbox it shows and checked/unchecked as it should, and it shows correctly in the front end and the back end.
But, when I try to query this setting with a wp_query to actually send the e-mails to those who haven't opted out, it bug out a little.
If the user opts out, then opts back in, the wp_query doens't pick them up. It only picks them up when I go into the wp-admin area and update their user profile. I don't actually have to change anything, just open the user up and click update.
Here's the wp_query just incase:
<?php $args = array(
'role' => 'Subscriber',
'meta_key' => 'planner-reminders',
'meta_value' => '0',
'meta_compare' => '=='
); ?>
<?php $user_query = new WP_User_Query( $args ); ?>
<?php if ( ! empty( $user_query->results ) ) : ?>
etc. etc.
Any ideas how I can get this working properly? Is there a function to fake a click on the 'Update User' button in wp-admin?
Thanks.
When the user opts out, the $field value will be empty, and therefore the value-attribute of the checkboxes will be empty. I haven't fully tested it, but this can yield unexpected behaviour in your setup. When a form with an unchecked checkbox is submitted through a POST request, the checkbox name will not be set in the $_POST array, which is why you should, in this case, set the value-attribute of the checkboxes to "1", so it is properly stored via update_field.
Could you try changing the checkbox values in the code posted above to "1" for the checkbox input elements? That would be value="1" instead of value="<?php echo $field; ?>".
To prevent PHP notices being generated for inexistent array keys, I would advise to change
update_field('planner-reminders', $_POST['planner-reminders'], 'user_'.$user_id.'');
to
update_field('planner-reminders', empty( $_POST['planner-reminders'] ) ? '0' : '1', 'user_'.$user_id.'');
as well.

wordpress backend checkbox won't save the value (checked)

I working on a theme options page for my template and want to integrate a checkbox. The problem is, the checkbox won't save the value. Where is my mistake? Well, I'm clueless...
<?php
add_action( 'admin_init', 'theme_options_init' );
add_action( 'admin_menu', 'theme_options_add_page' );
function theme_options_init(){
register_setting( 'wordpress_options', 'wordpress_theme_options');
}
function theme_options_add_page() {
add_theme_page('Options', 'Options', 'edit_theme_options', 'theme-options', 'wordpress_theme_options_page' );
}
function wordpress_theme_options_page() {
global $select_options, $radio_options;
if ( ! isset( $_REQUEST['settings-updated'] ) )
$_REQUEST['settings-updated'] = false; ?>
[...]
<?php if ( false !== $_REQUEST['settings-updated'] ) : ?>
<div class="updated fade">
<p><strong>Settings saved!</strong></p>
</div>
<?php endif; ?>
<form method="post" action="options.php">
<?php settings_fields( 'wordpress_options' ); ?>
<?php $options = get_option( 'wordpress_theme_options' ); ?>
<div id="admininterface">
[...]
<p>
<p class="before-form">Show RSS Icon:</p>
<?php $options = get_option( 'icon-rss-show' ); ?>
<input type="checkbox" name="$options[icon-rss-show]" value="1"<?php checked( 1 == $options['icon-rss-show'] ); ?> />
</p>
[...]
</div><!-- #admininterface -->
</form>
</div>
<?php } ?>
Adding another hidden input field will actually make the form save the zero value in the database like this:
<form>
<input type='hidden' value='0' name='selfdestruct'>
<input type='checkbox' value='1' name='selfdestruct'>
</form>
Make sure to place the hidden input above your regular one, with the value '0' and the same name as in the regular input field.
That answer is from here: https://stackoverflow.com/a/1992745/4688612
Please send all credit to that poster.

Call to undefined function update_post_meta()

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.

Categories