Wordpress IF statement not working - php

I have an IF statement stating the following...
<?php
if (empty($data['footer_text'])) {
echo'<p>© ';
print(Date("Y"));
echo'<span class="sep"> | </span><a href="';
echo get_settings('home');
echo'" title="';
bloginfo( 'name' );
echo'" rel="home">';
bloginfo( 'name' );
echo'</a></p>';
}
else{
echo'<p>';
global $data;
echo $data['footer_text'];
echo'</p>';
}
?>
The problem I'm running into is that when I call it like this.
<p><?php global $data; echo $data['footer_text']; ?>;</p>
It displays my text correctly. But when I use the IF statement it always defaults to the showing the site name even when I know it's displaying the text correctly.
Is my syntax screwed up? I can't figure out why it thinks nothing is there but still shows up when I display in a p tag.

You don't declare $data to be global until you're INSIDE the if(), meaning that $data is undefined at the point you're doing the
if (empty($data[...])) {
you probably want
global $data;
if (empty($data[...])) {
instead.

Try to debug the variable $data['footer_text']:
<?php var_dump($data['footer_text']); ?>
just before IF statement. Remember, when empty() returns true.

Related

Why my string variable recognized as empty?

I have an input with PHP Theme editor for Wordpress, and save it to some variable.
I want to render (echo) out the variable if is not an empty variable.
If I don't use If, it'll work okay (it is proof the variable not empty)
<div class="role"><?php echo get_post_meta( get_the_ID(), 'hcf_single_role_1', true );?></div>
But when I want to echo the only variable with value, it works otherwise
<?php if(!empty($post_id['hcf_single_role_1'])) { ?> //if not empty
<div class="name"><?php echo get_post_meta( get_the_ID(), 'hcf_single_role_1', true );?></div>
<?php } else { ?> //if empty
Variable Empty
<?php } ?>
if I change to if(empty()) it'll just work otherwise, my variable recognized as an empty variable.
*edit:
I also tried if(!empty($_POST['hcf_single_role_1'])), but it's the same
Based on #ChrisHaas comment, I tried this method, and it works
<?php if(!empty(get_post_meta( get_the_ID(), 'hcf_single_role_1', true ))) { ?> //if not empty
<div class="name"><?php echo get_post_meta( get_the_ID(), 'hcf_single_role_1', true );?></div>
<?php } else { ?> //if empty
Variable Empty
<?php } ?>
The method before I messed up between array and string variables.
So I clean up with the same method I echoed with get_post_meta() as the if condition

How to make a php function working inside an echo?

I tried adding HTML inside a PHP echo, and it works.
But the echo is not working if with a PHP function,
The original code:
if( ! function_exists("rframework_cart_button") ){
function rtframework_cart_button(){
$show_cart = get_theme_mod( RT_THEMESLUG.'_top_shortcut_buttons_cart' ) ? true : false ;
if( ! class_exists('Woocommerce') || ! $show_cart ){
return;
}
global $woocommerce;
echo '<li class="cart"><span class="icon-shopping-bag"><sub class="number empty">'. $woocommerce->cart->cart_contents_count .'</sub></span></li>'."\n";
}
}
The PHP code I want to put inside or after the echo is:
<?php $wishlist_count = YITH_WCWL()->count_products(); ?><span class=”your-counter-selector“><?php echo $wishlist_count; ?></span>
Can anyone help?
with echo, you need to change all the double quotes inside the html stuff to single quotes and keep double quotes outside.
Try this:
return "<li class='cart'><a href='#' class='cart-menu-button'><span class='icon-shopping-bag'><sub class='number empty'>{ $woocommerce->cart->cart_contents_count }</sub></span></a></li>'.'\n";
your question was quite confusing but I got an update based on your comment.
echo within a function called from another function will just output the echo to your page before the end of the other function, using return instead of echo will place that code snippet to the other function. it is the preferred way.
If in html, <?php functionWithEcho() ?> or <?php echo functionWithReturn()
if in php, call function normally
try this
echo "<li class='cart'><a href='#' class='cart-menu-button'><span class='icon-shopping-bag'><sub class='number empty'>'". $woocommerce->cart->cart_contents_count ."'</sub></span></a></li>.'\n'";
echo "<li class='heart'><a href='#' class='heart-button'><span class='icon-new-heart-1'><sub class='number empty'>0</sub></span></a></li>";

hello! I am new in php could please you tell me the answer. I am not getting Output for this PHP Program

why I am not getting output as a uid ?
this is my index.
<a href='my_posts.php?u_id=$user_id' class='list-group-item'>My Posts(<?php echo $post_count; ?>)</a>`
and this is my my_posts.php
<?php
include('function.php');
user_posts();
?>
and this is my function.php
<?PHP
function user_posts(){
global $con;
if(isset($_GET['u_id'])){
$u_id = $_GET['u_id'];
echo $u_id;
}
}
?>`
Maybe you should change you code like this:
<a href='my_posts.php?u_id=<?php echo $user_id;?>' class='list-group-item'>My Posts(<?php echo $post_count; ?>)</a>
`
Here's how your index should look:
<?php
$user_id = 12; // Example value
$post_count = 3; // Example value
echo "<a href='my_posts.php?u_id=$user_id' class='list-group-item'>My Posts($post_count)</a>";
You can remove the $user_id=12 and $post_count=3 in your implementation.
Your my_posts.php and function.php can stay as they are.
Edit: in your function.php you could additionally check if $_GET['u_id'] is of type integer using is_int()
There are several errors in your provided code
$user_id is not initialized anywhere, the function user_posts() echoes $u_id and not $user_id which you are printing.
Echoing inside a function is incorrect if you want to access its output, return should be used instead. Read more about return in php manual
<a href='my_posts.php?u_id=**$user_id**' class='list-group-item'>
This syntax is also incorrect as it is, php variables need to be present inside <?php ?> tags else they will be treated like simple text.
Solution:
<?php
include('function.php');
$user_id = user_posts(); // You can now use $user_id to print the output of user_posts()
echo "<a href='my_posts.php?u_id={$user_id}' class='list-group-item'>My Posts({$post_count})</a>";
?>
function.php
<?PHP
function user_posts(){
global $con;
if(isset($_GET['u_id'])){
$u_id = $_GET['u_id'];
return $u_id; // echo changed to return
}
}
?>
simply change single quotes to double quotes in anchor tag. In single quotes you can't put php variables.
<a href="my_posts.php?u_id=$user_id" class='list-group-item'>My Posts(<?=$post_count?>)</a>

simple if/else in PHP

I display the form validation error in codeigniter as below:
<?php echo form_error('name', '<div class="form_error">', '</div>'); ?>
i want to do it so that if there is error, then it should print error, otherwise it should print the info div.
For example,
if form_error, then
<?php echo form_error('name', '<div class="form_error">', '</div>'); ?>
else
<div class="info">Your first and last name. </div>
As form_error is not just a simple variable that i can check if it is empty then print info. How can i do it? Thanks.
You can do something like this:
if ( form_error('name') )
{
echo form_error('name');
}
For form_error might not be a variable but it's a function that returns a string. If the string is empty (NULL, FALSE, "", 0, ...), the if statement will fail (meaning there is no error) and the form_error('name') won't be called.
This sould do it :
if (form_error('name')){
echo form_error('name', '<div class="form_error">', '</div>');
} else {
echo '<div class="info">Your first and last name. </div>';
}

WordPress Conditional PHP Question

I'm trying to call WordPress's 'the_time' using a conditional statement that checks the category. I want to call the custom field 'event_date' if the category is '3' and 'the_time()' if the category is '4'... This is what I have, and it echoes fine if I use "is_single()" instead of "is_category()" but for some I'm getting no echo when I use "is_category()"... any ideas?
<?php
if (is_category('4')) {
echo "<span>";
the_time('');
echo "</span>";
} elseif (is_category('3')) {
echo "<span>";
get_post_meta ('event_date');
echo "</span>";
} else {
echo "<p>Pedro offers you his protection.</p>";
} ?>
In this instance you want to use in_category instead of is_category.

Categories