I am trying to write a function that has an if condition to first see which option of an input is checked and then to check that value. For example, in the code below, I have a radio input named "term_id". I want to first get the option that is checked and then check the value. And then if that value is 70, for example, then do something. I also want to accomplish this with php if possible and not jquery.
$terms = get_terms( 'heythere', $args );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
echo '<ul class="form-ul">';
$count = 0;
foreach ( $terms as $term ) {
$count++;
$t_id = $term->term_id;
$term_meta = get_option( "taxonomy_$t_id" );
?>
<li>
<label>
<input type="radio" name="term_id" value="<? php echo $term->term_id; ?>" <?php if ($count == 1) { echo 'checked="checked"'; } ?>>
</label>
</li>
I a very new to php and this is my go at it but I am sure it is incorrect.
if(isset($_GET[term_id]) && ($term->term_id === '70')) {
DO THIS
}
Related
I have a problem with updating my custom meta box in wordpress for a custom post news. I have made a meta box checkbox so I could decide if the post is featured or not. If the checkbox is checked then isFeatured = 1. Until this part it works, the problem is when after sometime I wan't to edit the post and deselect the checkbox. After updating the post the checkbox is checked all the time.
The database: meta_key | meta_value ---- > news_details | a:1:{s:10:"isFeatured";s:1:"1";}
This is how my code looks:
function add_news_details_meta_box() {
add_meta_box(
'news_details_meta_box', // $id
'Checkbox', // $title
'show_news_details_meta_box', // $callback
'news', // $screen
'normal', // $context
'high' // $priority
);
}
add_action( 'add_meta_boxes', 'add_news_details_meta_box' );
function show_news_details_meta_box() {
global $post;
$meta = get_post_meta( $post->ID, 'news_details', true ); ?>
<input type="hidden" name="news_details_meta_box_nonce" value="<?php echo wp_create_nonce( basename(__FILE__) ); ?>">
<p>
<label for="news_details[isFeatured]">Is this post featured?
<input type="checkbox" name="news_details[isFeatured]" value="1" <?php if (is_array($meta) && $meta['isFeatured'] === '1' ) { echo 'checked';} else { echo 'Unchecked';}?>>
</label>
</p>
<?php
}
function save_news_details_meta( $post_id ) {
// verify nonce
if ( !wp_verify_nonce( $_POST['news_details_meta_box_nonce'], basename(__FILE__) ) ) {
return $post_id;
}
// check autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
// check permissions
if ( 'page' === $_POST['post_type'] ) {
if ( !current_user_can( 'edit_page', $post_id ) ) {
return $post_id;
} elseif ( !current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
}
$old = get_post_meta( $post_id, 'news_details', true );
$new = $_POST['news_details'];
if ( $new && $new !== $old ) {
update_post_meta( $post_id, 'news_details', $new );
} elseif ( '' === $new && $old ) {
delete_post_meta( $post_id, 'news_details', $old );
}
}
add_action( 'save_post', 'save_news_details_meta' );
Could you explain to me what's wrong with updating the checkbox after sometime?
I think you should check the if condition. The way I got your description, the $meta['isFeatured'] === '1' always becomes true. You set the value to 1 in the input tag. So on save the value of 1 gets saved to the field I guess. On next load you are checking with an if, if the $meta['isFeatured'] is 1, so this is always true.
You could check if the checkbox in the form is checked with:
isset($_POST['name_of_your_checkbox_input'])
If it is set, you save the value of 1. If not, you stay at the pre defined value of 0.
What worked for me in this case was changing } elseif ( '' === $new && $old ) { to } else ( '' === $new && $old ) {
I'm adding a custom field to page that would select a blog category to feature on that page. All the other fields are working fine, so, i'll post code related only to this field. Getting error 500 when loading page editor.
$feat_blog = isset( $values['feat_blog'] ) ? esc_attr( $values['feat_blog'][0] ) : "";
This is the field itself
<select name="feat_blog" id="feat_blog" value="<?php echo $feat_blog; ?>">
<?php $categories = get_categories(); foreach($categories as $category) { ?>
<option value="<?php echo $category->slug ?>"> <?php echo $category->name ?></option>
<?php } ?>
</select>
And sanitization code that's actually causing trouble
if ( isset( $_POST['feat_blog'] )){
$valid_values = array(
categories = get_categories();
foreach($categories as $category) {
echo $category->slug,
}
);
$value = sanitize_text_field( $_POST['feat_blog'] );
if( in_array( $value, $valid_values ) ) {
update_post_meta( $post->ID, 'feat_blog', $value );
}
}
There are quite a few things wrong with your code here...
You are putting ; in an array and attempting to assign a variable there.
You are echoing in an array.
You are running functions inside an array.
You are missing the $ infront of the $categories variable.
if ( isset( $_POST['feat_blog'] )) {
$categories = get_categories();
$valid_values = array();
foreach($categories as $category) {
$valid_values[] = $category->slug;
}
$value = sanitize_text_field( $_POST['feat_blog'] );
if( in_array( $value, $valid_values ) ) {
update_post_meta( $post->ID, 'feat_blog', $value );
}
}
What I did here is I moved the $categories variable declaration outside of the foreach loop and set the $valid_values array BEFORE the loop. If you set it inside the loop it is going to reset every time the loop occurs. You also can't use ; inside of an array as that is intended to close the statement.
I am using the following code to output a list of custom taxonomies:
<?php // Get terms for post
$terms = get_the_terms( $post->ID , 'status' );
// Loop over each item since it's an array
if ( $terms != null ){
foreach( $terms as $term ) {
// Print the name method from $term which is an OBJECT
print $term->slug ;
print $term->name;
// Get rid of the other data stored in the object, since it's not needed
unset($term);
}
}
?>
My question is, how can I add html to this loop? I have tried multiple methods such as:
echo '<button class="filter $term->slug" data-filter="$term->slug">$term->name</button>';
...but this either errors out, or doesn't print the required terms. My desired output of html would be:
<button class="filter term-slug" data-filter="term-slug">term-name</button>
Please try this way. It should work. You need to just print '".$term->name."'like this inside the HTML to identify the object variable.
<?php // Get terms for post
$terms = get_the_terms( $post->ID , 'status' );
// Loop over each item since it's an array
if ( $terms != null ){
foreach( $terms as $term ) {
// Print the name method from $term which is an OBJECT
echo '<button class="filter "'.$term->slug.'"" data-filter="'.$term->slug.'">'".$term->name."'</button>';
// Get rid of the other data stored in the object, since it's not needed
unset($term);
}
}
?>
Rewrite your line as below:-
echo "<button class='filter {$term->slug}' data-filter='{$term->slug}'>$term->name</button>";
Please add below code and check.
<?php
$terms = get_the_terms( $post->ID , 'status' );
if ( $terms != null )
{
foreach( $terms as $term )
{
echo '<button class="filter "'.$term->slug.'"" data-filter="'.$term->slug.'">'".$term->name."'</button>';
unset($term);
}
}
?>
I'm using WP_Query to get posts from a custom post type to use the result in a metabox. Everything works great with my query. But after this query I can't get the other meta values from database.
This is my helper function to get custom field value:
function my_page_get_custom_field( $value ) {
global $post;
$custom_field = get_post_meta( $post->ID, $value, true );
if ( !empty( $custom_field ) )
return is_array( $custom_field ) ? stripslashes_deep( $custom_field ) : stripslashes( wp_kses_decode_entities( $custom_field ) );
return false;
}
Here's my query:
$sliderArgs = array(
'posts_per_page' => -1,
'post_type' => 'slider',
);
$slider = new WP_Query($sliderArgs);
if ($slider->have_posts()) {
?>
<select name="slider" id="slider">
$selectedSlide = my_page_get_custom_field('slider');
while($slider->have_posts()){
$slider->the_post();
$slideID = get_the_ID();
?><option value="<?php echo $slideID; ?>" <?php selected($selectedSlide, $slideID, true); ?>><?php the_title(); ?></option><?php
}
wp_reset_postdata(); ?>
</select>
}
And this is my other custom field which returns empty (there is a value in database and when I try to change it works great but not displaying in input value in admin):
<input type="text" name="meta_title" id="meta_title" value="<?php echo my_page_get_custom_field('meta_title'); ?>">
OK I solved it.
I used get_posts instead of WP_Query. This helped me a lot: https://core.trac.wordpress.org/ticket/18408#comment:5
I'm trying to edit a WordPress front end user form plugin so that it will write away multiple check box values (from one of my custom fields) in a single SQL record. I need to do this so my query search form can search for all values checked off. Right now, if a user checks off more than one option, only one single value is being stored. How can I adjust this PHP code so that every value checked will be stored?
/**
* Prints a checkbox field
*
* #param array $attr
* #param int|null $post_id */
function checkbox( $attr, $post_id, $type ) {
$selected = isset( $attr['selected'] ) ? $attr['selected'] : array();
if ( $post_id ) {
$selected = explode( self::$separator, $this->get_meta( $post_id, $attr['name'], $type, true ) );
}
?>
<div class="wpuf-fields">
<span data-required="<?php echo $attr['required'] ?>" data-type="radio"></span>
<?php
if ( $attr['options'] && count( $attr['options'] ) > 0 ) {
foreach ($attr['options'] as $option) {
?>
<label>
<input type="checkbox" name="<?php echo $attr['name']; ?>[]" value="<?php echo esc_attr( $option ); ?>"<?php echo in_array( $option, $selected ) ? ' checked="checked"' : ''; ?> />
<?php echo $option; ?>
</label>
<?php
}
}
?>
Thanks in advance!