How can I get in_array() to work with get_option()? - php

I have two custom fields in my admin settings that allow me to enter two sets of valid zip codes ('woocommerce_local_delivery_zone_a' and 'woocommerce_local_delivery_zone_b').
I cannot figure out why this code isn't working:
$codes_a = get_option( 'woocommerce_local_delivery_zone_a' );
$codes_b = get_option( 'woocommerce_local_delivery_zone_b' );
if ( in_array( "55410", array( $codes_a ) )) {
echo "Codes A";
}
if ( in_array( "55410", array( $codes_b ) )) {
echo "Codes B";
}
If I echo $codes_a or $codes_b (outside of the "if" statement), the string of zip codes returns on the page. If I take that string and enter it directly where the variable is in if (in_array( "55410", array( NUMBERS HERE ))) the text I am trying to echo (Codes A or Codes B) works.
I also tried placing array( get_option( 'field_name')) directly in the if statement, and that didn't do anything either. Does in_array not work with getting admin custom fields?

If $codes_a is a string like this: 08847,032434,35545 then you need to use explode() to turn it into an array. I suspect what you really want is this:
if ( in_array( "55410", explode( ',', $codes_a ) ) {
echo "Codes A";
}

Related

WordPress search returning duplicate results

There is a search box on a WordPress site I'm currently working on that is returning somewhat sporadic duplicate results based on the number of keywords entered into the search. For example, if I enter the keywords "Green Lake" into the box, it will return Post A twice, Post B twice, Post C once or twice. If I enter "Green Lake Lake" (three keywords), it will return Post A three times, Post B three times, Post C two or three times. Etc etc.
I think I've narrowed the issue down to the following code. Can anyone point out anything obvious? I think it has something to do with the foreach loop on $keywords, but I don't know how to fix it. Thank you in advance.
if ( isset( $_REQUEST['keywords'] ) && ! empty( $_REQUEST['keywords'] ) ) {
$filter_posts = [];
$keywords = explode( ' ', strtolower( $_REQUEST['keywords'] ) );
foreach ( $posts as $post ) {
foreach ( $keywords as $keyword ) {
// if the keyword is in the title the don't search the post content
if (
strpos( strtolower( $post->post_title ), $keyword ) !== false
||
strpos( strtolower( $post->post_content ), $keyword ) !== false
) {
$filter_posts[] = $post;
}
}
}
$posts = $filter_posts;
}
wp_send_json_success( json_encode( $posts ) );

Tags with WooCommerce and Custom Code

I'm no php pro (probably not even the level of enthusiast) but I can usually get my head around some PHP. I'm working on a WordPress install that's running WooCommerce. This piece of code is plaguing me – I need to pull the tags for an individual post from the database, and if the system finds a tag that matches the rules I will define in the if statement, it will display a particular piece of HTML. I was attempting to use the in_array function to search for a particular value that I know (in this case "games") as part of the array produced from wp_get_post_terms( $post->ID, "product_tag" ). When I print the results I can clearly see that "games" is a value in the array, but the function only outputs the result of the else statement.
I thought this code would do the trick, as it seems pretty simple, but alas, so far, I have had no success. Someone a little brighter when it comes to PHP and WP have some ideas?
<?php
$terms = wp_get_post_terms( $post->ID, "product_tag" );
print_r ( $terms );
if ( in_array( "games", $terms ) ) {
echo '<img src="http://www.myurl.com/images/image.png" alt="games" />';
} elseif ( in_array( "bulk" , $terms) ) {
echo 'Hello bulk buyer!';
} else {
echo 'Howdy!';
}
?>
To return an array of term names, override the default args of wp_get_post_terms() as follows:
wp_get_post_terms( $post->ID, "product_tag", array( "fields" => "names" ) );
https://codex.wordpress.org/Function_Reference/wp_get_post_terms

Wordpress Theme Development

what i'm currently trying to achieve is some simple theme options. What i want to b able todo is;
Under the Wordpress GUI, Theme options section which i've currently got a few setup (with no actions behind them), is to simply "Click a check box which will disable front page widgets, if it's on then show them if it's off disable them"
I understand the logic behind, but i have no idea on how to create something will will return a simple true of false, from the theme-options.php, link it into the front-page.php and use the result from theme-options to be the condition to show said area.
Any help with this would be amazing.
<?php $options = get_option( 'SV_theme_options' ); ?>
<input id="SV_theme_options[option1]" name="SV_theme_options[option1]" type="checkbox" value="1" <?php checked( '1', $options['option1'] ); ?> />
/**
* Sanitize and validate input. Accepts an array, return a sanitized array.
*/
function theme_options_validate( $input ) {
global $select_options, $radio_options;
// Our checkbox value is either 0 or 1
if ( ! isset( $input['option1'] ) )
$input['option1'] = null;
$input['option1'] = ( $input['option1'] == 1 ? 1 : 0 );
// Say our text option must be safe text with no HTML tags
$input['sometext'] = wp_filter_nohtml_kses( $input['sometext'] );
// Our select option must actually be in our array of select options
if ( ! array_key_exists( $input['selectinput'], $select_options ) )
$input['selectinput'] = null;
// Our radio option must actually be in our array of radio options
if ( ! isset( $input['radioinput'] ) )
$input['radioinput'] = null;
if ( ! array_key_exists( $input['radioinput'], $radio_options ) )
$input['radioinput'] = null;
// Say our textarea option must be safe text with the allowed tags for posts
$input['sometextarea'] = wp_filter_post_kses( $input['sometextarea'] );
return $input;
}
Also in the front-page.php I've required_once for this file, tried calling by doing:
echo theme_options_validate( $input['option1' );
however all i get is the word Array returned.
Regards,
Ben
Maybe that's because you made a typo.
You didn't close the bracket
echo theme_options_validate( $input['option1' );
Should be:
echo theme_options_validate( $input['option1'] );

Not managing to find a way to use is_page() with a variable inside storing an array

Building a Wordpress plugin here. I have a text form in which user can insert pages IDs - separated by commas - to display a BAR in it.
Then I set a variable to get those Ids and I am trying to use this variable with the conditional tag "is_page( array() )".
Problem is, the BAR is showing only on the page related to the first ID given by the user. It is strange, cause I echoed the variable and it is showing all Id's given, separated by commas. Hardcoding the IDs, like this "is_page( array(207,306) )", I get the result I want and the BAR is displaying on the 2 pages related to the IDs. But when I use "is_page( array($setting_value) )", only page related to the first ID is showing the BAR, even when "echo $setting_value" results in 207,306.
Can anybody show me what is wrong?
Here's the parts of the code related to it:
//getting the IDs
$setting_value = esc_attr( (get_option( 'ma-singular' ) ) );
if ( $home != 'onlyHome' ) {
echo "<input class='text' type='text' name='ma-singular' value='" . $setting_value . "' />";
}
//displaying the BAR on the PAGES related to the IDs typed by users
$singular = esc_attr (get_option ( 'ma-singular' ));
if ( $home != 'onlyHome' && is_page( array($singular) ) ) {
if ( is_user_logged_in() && !empty($alttext) ) {
echo '<div class="ma-' . $cor = get_option( 'ma-cor') .
'" style="background-color:' . $cordefundo = get_option( 'ma-fundo' ) .
'">' . $alttext . '</div>';
}
You need to explode $singular before using it. It's going wrong because what you think is an array of values is a single value inside the array.
Let's say for example 'ma-singular' = 127,135,189.
array( $singular ) would be equal to array( '127,135,189' ) when what you need is array( 127, 135, 189 ).
So to fix this you would do:
$singular = esc_attr (get_option ( 'ma-singular' ));
if ( ! empty( $singular ) ) {
$singular = explode( ',', $singular );
}
if ( $home != 'onlyHome' && is_page( $singular ) ) {

Wordpress: Conditional function (if/else) for custom tag

I am using a custom type tag named "price_tag" that to display the list of them, I use this :
<?php if(get_the_term_list($post->ID, 'price_tag')) echo get_the_term_list($post->ID, 'price_tag', '', ' - ', '' ); else echo __('No Tags', 'custom-lang'); ?>
What I want is to create a condition using the "price_tag" to display a text if the post has a certain " price_tag"
so I wanted to try this
<? if ( has_tag ('certaintag' ) ) { ?>
Show something
<? } else {} ?>
"cetainbtag" is just a tag that I want to use to show a text in all posts that have that tag.
But I cannot get it to work and as I am not wordpress techie , I dont know how to include the " price_tag " to this conditional.
Please help. I be grateful.
You can use wordpress's wp_get_post_tags($post->ID); to get all the tags of the post it will return the array and you have to loop through the array to get the tags
$tags=wp_get_post_tags($post->ID);
$alltags=array();
foreach($tags as $t){
$alltags[]=$t->taxonomy;
}
Then use inarray in you if condition
if (in_array("certaintag", $alltags)) {
Show something
}else{
}
Hope it makes sense
The prototype of has_tag is
has_tag( $tag, $post );
If you are in the loop then you can use
if( has_tag( 'price_tag' ) ) {
// price_tag exists, do your job here
}
Above code will check, if current post has price_tag tag attached to it, you can also pass an array for multiple tags, i.e.
if( has_tag( array('price_tag', 'another_tag') ) ) {
// ...
}
If you want to check whether price_tag exists from outside of the loop then you can use
if( has_tag( 'price_tag', $post ) ) {
// price_tag exists, do your job here
}
In this case $post is the $post object that you've queried. Look at has_tag on Codex and this post on WordPress forum too.
You can also use has_term( $term, $taxonomy, $post ), i.e.
if( has_term( 'price_tag', 'post_tag' ) ) {
// do something
}
Also, term_exists( $term, $taxonomy, $parent ) could be used as given below
$term = term_exists( 'price_tag', 'post_tag' );
if ( $term !== 0 && $term !== null ) {
// The term 'price_tag' is used as a post tag;
}
Above code will return the id of the term (price_tag in this case) if it exists.
Also check get_the_tags and get_tags on Codex and these WordPress answers (this one and this one) too, hope you can find a solution from anyone of these.
I use Dianaj's approach but with some syntax changes:
$tags = get_the_tags($post->ID);
if ($tags) {
foreach ($tags as $t) {
$tag_array[] = $t->slug;
}
if (in_array('certaintag',$tag_array)) {
//Do something
} else {
//Do something else
}
}
If you put this snippet inside a function, don't forget to add global $post; before the first line of code. Also note I use the tag's slug rather than name. I find slugs are easier to use in conditionals than the tag name.

Categories