How to combine two if statements in PHP for WP - php

I need to run a function (show a modal) if a page DOES have a particular field value AND is not a certain page. It also needs to work if the page DOES NOT HAVE THE FIELD VALUE and it is not the certain page. They work individually like this:
//if page has field value:
<?php
$values = get_field( 'recast_video' );
if ( ($values) ) {
get_template_part('template-parts/popIn', 'none');
}
?>
//if it is this page, do not show:
<?php
if ( (!is_page('trading-education-webinars') ) ){
get_template_part('template-parts/popIn', 'none');
}
?>
How do I combine the two so that it shows (get_template_part) if the field has the value and is not the specified page
//this fails
<?php
$values = get_field( 'recast_video' );
if ( ($values) || (!is_page('trading-education-webinars') ) ){
get_template_part('template-parts/popIn', 'none');
}
?>

First thing to do is look for the common condition. No matter what, you don't want it to be a certain page, right? So first check that it's not that page:
$result = (!is_page('trading-education-webinars')) ? : ;
It also needs to work if the page DOES NOT HAVE THE FIELD VALUE and it
is not the certain page.
Then why check for the field value at all? No need, as long as it's not the page we checked for.
$result = (!is_page('trading-education-webinars')) ? get_template_part('template-parts/popIn', 'none') : return false;
You can change return false; to whatever you want to happen if the page IS 'trading-education-webinars'
EDIT: Clarifying the conditional:
Page X never gets served "content" (modal)
If NOT page X and has $values, show content
If NOT page X and NOT has $values, show some other content
$values = get_field( 'recast_video' );
$x = get_template_part('template-parts/popIn', 'none');
$y = 'some other content';
$return = (!is_page('trading-education-webinars')) ? (($values) ? $x; : $y ) : return false );

Use the && (AND) operator:
<?php
$values = get_field( 'recast_video' );
if ( ($values && !is_page('trading-education-webinars')) || (!$values && is_page('trading-education-webinars')) ){
get_template_part('template-parts/popIn', 'none');
}
?>
|| operator is true when at least one or both statements are true

Related

create a function and modify a post on the wordpress site based on the post id

I'd want to edit the content of one of my posts using this way, but it's not working.
Does this filter affect the php output or the raw php file?
add_filter( 'the_content', 'multiple_string_replacements');
function multiple_string_replacements ( $contentt ) {
if ( is_single('15467') && in_the_loop() && is_main_query() ) {
$condition = true;
if($condition){
$urlsdothejob = "link.com";
$text = array(
"$variable1" => "$urlsdothejob",
"$variable2" => "$urlsdothejob",
);
$contentt = str_ireplace(array_keys( $text ), $text, $contentt);
};
}
return $contentt;
}
If you know the conditions that you're looking for, you should be able to write an IF statement to match.
if ($constantvalue == "anothervalue") {
// make local changes
}
Can you give an example of what you're trying to do? It's hard to understand the request with such little information.
Try like this hope its working for you.
while(have_posts()):
$ID = get_the_ID();
$data1 = get_post_meta($ID,'meta_key',true);
$data2 = get_post_meta($ID,'meta_key',true);
if($data1 == $data2) {
update_post_meta($ID,'meta_key','meta_value');
}
endwhile;

PHP Not Running On Page Until Refresh

bit of a strange one that I've not been able to resolve for months so I finally have given in and have come here for the answer. Hopefully.
I have the below shortcode that when ran returns the phone number depending on what variable has a value. This PHP code works as expected, the one thing that doesn't work as expected however, is the first ever page load.
When someone goes to the site for the first time (or in incognito mode) the shortcode doesn't output anything, however, refresh the page just once and it'll display the output of the correct value and I have no idea why.
<?php function gw_cookie($atts) {
extract(shortcode_atts(array(
"value" => ''
), $atts));
$getVariable = isset($_GET[$value]) ? $_GET[$value] : '';
$newGetVariable = str_replace('_', ' ', $getVariable);
$cookiePhone = isset($_COOKIE[$value]) ? $_COOKIE[$value] : '';
$acfField = get_field('page_cookie_'.$value.'');
$optionsACF = get_field('options_company_details', 'options');
$area = $optionsACF['options_area'];
$phone = $optionsACF['options_telephone'];
if(!empty($cookiePhone) && $cookiePhone !== 'undefined') { //If cookie is not empty do the following
echo '1';
} elseif(!empty($newGetVariable)) { //If cookie is empty and get variable is not
echo '2';
} elseif($acfField) { //If ACF field is not empty do the following
echo '3';
} elseif ($value == "phone") {
return '4';
}
} add_shortcode("gw-cookie", "gw_cookie");
This codes file is being imported into the functions.php file using the following line:
require_once( __DIR__ . '/shortcodes/gw-cookies.php' );
A cookie itself would be created on the first run and your criteria requires cookiePhone which is why you have to refresh to make it work.
As per the comments, change:
$cookiePhone = isset($_COOKIE[$value]) ? $_COOKIE[$value] : '';
to:
$cookiePhone = isset($_COOKIE[$value]) ? $_COOKIE[$value] : NULL;

php same parameters different value

I have a Wordpress plugin that when searching multiple types of cars ads multiple values of the same parameter in the url.
/home/?condition=filter1&condition=filter2&s=
Now I created multiple user roles for different cars and want to add a filter to the site.
This code works with 1 value
function wprdcv_param_redirect() {
$user = wp_get_current_user();
$url_parts = parse_url("http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']);
parse_str($url_parts['query'], $params);
if( ! empty( $user ) && count( array_intersect( [ "userrole1" ], (array) $user->roles ) ) ) {
//Prevent infinite loop
if ($_GET["condition"] == "filter1") return;
$params["condition"] = "filter1";
$params["s"] = "";
}
else {
//Prevent infinite loop
if ($_GET["condition"] == "") return;
$params["condition"] = "";
$params["s"] = "";
}
foreach (array_keys($_GET) as $n) {
$params[$n] = $_GET[$n];
}
// Note that this will url_encode all values
$url_parts['query'] = http_build_query($params);
wp_redirect(http_build_url($url_parts));
exit;
}
add_action('template_redirect', 'wprdcv_param_redirect');
When I add the following it offcourse overwrites the value and only shows the last one:
if ($_GET["condition"] == "filter1") return;
$params["condition"] = "filter1";
$params["condition"] = "filter2";
$params["s"] = "";
how can I edit this so it shows me 2 same parameters with different values?
EDIT:
I have adding following and it works like a charm!
$params["condition"][] = "Filter1";
$params["condition"][] = "Filter2";
Thx fellas! #CBroe & #Vincent Decaux
/home/?condition%5B0%5D=Filter1&condition%5B1%5D=Filter2&s= proves to be perfectly working

problem with else if statement not working

I have created a custom WordPress filter. I am importing api data to a form. My first IF statement works (when district not empty). But I can't get the else if working to populate the 2nd field. The 2nd result in the JSON has no value for district. What I am trying to do is separate the results to display House rep in one field and Senators in another. (There are actually two Senate results but I don't now how to separate the results). Each result needs to be in different form fields.
add_filter('frm_pre_create_entry', 'import_fields');
function import_fields($values){
if ( $values['form_id'] == 12 ) {
$zipcode = $values['item_meta'][71];
$api_request = 'http://whoismyrepresentative.com/getall_mems.php?zip='.$zipcode.'&output=json';
$api_response = wp_remote_get( $api_request );
$api_data = json_decode( wp_remote_retrieve_body( $api_response ), true );
foreach ( $api_data["results"] as $member ) {
if ( $member["district"] != '' ) {
$values['item_meta'][72] = $member['name'];
$values['item_meta'][73] = $member['phone'];
$values['item_meta'][74] = $member['office'];
} else if ( $member["district"] == '' ) {
$values['item_meta'][75] = $member['name'];
$values['item_meta'][76] = $member['phone'];
$values['item_meta'][77] = $member['office'];
}
break;
}
}
return $values;
}
I am new here, please go easy on me.
just like #billyonecan said, that break looks weird.
PS: You don't even need to specify the 'else if' condition... simply replace the 'else if' by 'else'
...
} else if ( $member["district"] == '' ) {
...
becomes
...
} else {
...
In your case it won't make any difference, it's just good practices

How to define condition based on get url value

When a user clicks on "List View" link then I want to show them the "List View" HTML and when they click on "Grid View" I want to show "Grid View" HTML.
I've defined the following links to click-
List View <br>
Grid View
Then I defined the following condition with PHP get method to show user the desired output-
<?php
if( isset( $_GET['view'] ) == 'list' ){
echo "This is List view";
}else if( isset($_GET['view'] ) == 'grid' ){
echo "This is Grid view";
}
This condition is not working. If I change "view" to "view_1" and "view_2" from URL then my condition is working as well.
<?php
if( isset( $_GET['view_1'] ) == 'list' ){
echo "This is List view";
}else if( isset($_GET['view_2'] ) == 'grid' ){
echo "This is Grid view";
}
?>
<br>
List View <br>
Grid View
But I don't want to change the "view" key. I just want to keep the same key and different value for both URL to do the conditional statement.
Is it possible?
isset() only checks if a variable exists and isn't null and returns a boolean (true/false).
To check the value, you first need to check if it exists (isset) and then check the value, like this::
if (isset($_GET['view']) && $_GET['view'] == 'list') {
// your code here.
}
You can read more here: http://php.net/manual/en/function.isset.php
Alternative
If you want to make your code and conditions slightly more readable, you could to this:
// Get the value of $_GET['view'] once, if it exists (pre PHP 7)
$view = isset($_GET['view']) ? $_GET['view'] : null;
// PHP 7.x (new shorter syntax for the above)
$view = $_GET['view'] ?? null;
if ($view == 'list') {
// your code
}
Did you try like this..
if(isset($_GET['view']))
{
if($_GET['view'] == 'list')
{
//code here
}
elseif ($_GET['view'] == 'grid')
{
//code here
}
}

Categories