What Iam trying to create if possible within WordPress or via just php is that i have functions such as below.
function myfunction_displays_something_one() {
echo "string";
echo "string"; }
function myfunction_displays_something_two() {
echo "string";
echo "string";
}
function myfunction_displays_something_three( $arg1, $arg2 ) {
echo "<p>";
echo $arg1 $arg2;
echo "/<p>";
}
I would like the functions above to be called and displayed via
function myfunction_displays( $display ) {
if ( $display == 'something-one' )
return myfunction_displays_something_two();
if ( $display == 'soemthing-two' )
return myfunction_displays_something_two();
}
when called vai the php file would like to call the three functions above like so
<?php myfunction_displays( something-two ); ?>
<?php myfunction_displays( something-three ); ?>
i have been able to make the first two work because they have no arguments but I am unable to call the third function because it has arguments.
Is there a way to create this maybe using Wordpress Filters or just php ?
Online DEMO : https://eval.in/87273
function myfunction_displays( $display ,$arrArg = array()) {
if ( $display == 'something-one' )
return myfunction_displays_something_two();
if ( $display == 'soemthing-two' )
return myfunction_displays_something_two();
if ( $display == 'soemthing-three' )
{
$a1 = isset($arrArg[0]) ? $arrArg[0] : "";
$a2 = isset($arrArg[1]) ? $arrArg[1] : "";
return myfunction_displays_something_three($a1,$a2);
}
}
And call it like this:
<?php myfunction_displays( 'something-two' ); ?>
<?php myfunction_displays( 'something-three',array(0=>'value1',1=>'value2')); ?>
Here is a quick way:
function myfunction_displays( $display, $arg1=NULL, $arg2=NULL ) {
if ( $display == 'something-one' )
return myfunction_displays_something_two();
if ( $display == 'something-two' )
return myfunction_displays_something_two();
if ( $display == 'something-three' )
return myfunction_displays_something_three($arg1, $arg2);
}
The "=NULL" part means you don't have to fill them in every time you call it.
An example:
<?php myfunction_displays( 'something-two' ); ?>
<?php myfunction_displays( 'something-three', 'value1', 'value2' ); ?>
Related
We are trying to break up a form into several pages using jQuery steps. The error points to the form that we're trying to create. Call to the form initially looks like this:
$enable_paid_submission = houzez_option('enable_paid_submission');
$user_pack_id = get_the_author_meta( 'package_id' , $userID );
$remaining_listings = 0;
if( is_page_template( 'submit_property_test.php' ) ) {
if( $enable_paid_submission == 'membership' && $remaining_listings != -1 && $remaining_listings < 1 ) {
print '<div class="user_package_status"><h4>'.esc_html__('HTMLhere.','houzez' ).'</h4></div>';
<?php
$layout = houzez_option('property_form_sections');
$layout = $layout['enabled'];
if ($layout): foreach ($layout as $key=>$value) {
switch($key) {
case 'features':
get_template_part( 'features' );
break;
case 'location':
get_template_part( 'location' );
break;
case 'multi-units':
get_template_part('multi-units');
break;
}
}
endif;
?>
We would like to break the three sections of the form (features, location and multi-units) into three different pages. We added jQuery steps and it now looks like the following:
<script>
$(function ()
{
$("#wizard").steps({
headerTag: "h2",
bodyTag: "section",
transitionEffect: "fade"
});
});
</script>
<div id="wizard">
<h2>Features</h2>
<section>
<?php
$layout = houzez_option('property_form_sections');
$layout = $layout['enabled'];
if ($layout): foreach ($layout as $key=>$value) {
switch($key) {
case 'description-price':
get_template_part('features');
break;
}
}
endif;
?>
<h2>Location</h2>
<section>
<?php
$layout = houzez_option('property_form_sections');
$layout = $layout['enabled'];
if ($layout): foreach ($layout as $key=>$value) {
switch($key) {
case 'description-price':
get_template_part('location');
break;
}
}
endif;
?>
</section>
<h2>Multi-units</h2>
<section>
<?php
$layout = houzez_option('property_form_sections');
$layout = $layout['enabled'];
if ($layout): foreach ($layout as $key=>$value) {
switch($key) {
case 'description-price':
get_template_part('multi-units');
break;
}
}
endif;
?>
</section>
It was running okay in the first few hours. Now it is returning the error.
It looks like you are expecting the the function call to houzez_option() to return an array. It would seem from the error that you are getting that it is not. Without seeing what the houzez_option() code looks like it is impossible to tell you why it is not.
You could still improve your code some by having it check specifically for what you expect to be returned from your function.
$layout = isset( $layout['enabled'] ) ? $layout['enabled'] : '';
if ( is_array( $layout ) ): foreach ($layout as $key=>$value) {
switch($key) {
case 'description-price':
get_template_part('features');
break;
}
}
else:
echo 'Unable to retrieve options';
endif;
This will prevent the error and alert you that it isn't working on the frontend. You could also make the message clearer for the enduser. I was just providing a generic example.
I've edited this question, because I felt I needed to be clearer in my request.
The code below relates to options checked in a checkbox. It will echo 'both values' when they are both checked, and it will echo 'only valueA', I cannot get it to echo 'only valueB' which instead prints blankly with no echo. Thoughts?
<?php
if (( $a == "General" ) && ( $b == "Specialist" )) {
echo '<h2>both values are printed in HTML</h2>';
}
?>
<?php
if (( $a == "General" ) && ( $b != "Specialist" )){
echo '<h2>only valueA is printed in HTML</h2>';
}
?>
<?php
if (( $a != "General" ) && ( $b == "Specialist" )) {
echo '<h2>only valueB is printed in HTML</h2>';
}
?>
If you want to check whether a value is in array you should use in_array function. Your code should look like this:
<?php
$CheckBox = $form_data['field'][1]
$a = ( "valueA");
$b = ( "valueB");
$isAChecked = in_array($a, $CheckBox[1]);
$isBChecked = in_array($b, $CheckBox[1]);
?>
<?php
if ( $isAChecked && $isBChecked ) {
echo '<h2>both values are printed in HTML</h2>';
} else if ( $isAChecked ) {
echo '<h2>only valueA is printed in HTML</h2>';
} else if ( $isBChecked ) {
echo '<h2>only valueB is printed in HTML</h2>';
} else {
echo '<h2>nothing printed</h2>'
}
?>
Code ONE (WORK ARIGHT):
function Hello( $rel ) {
$res = mysqli("SELECT * FROM TABLE");
$result = $res->num_rows;
if ( $rel == 1 ) {
print $result;
} elseif ( $rel == 2 ) {
echo $result;
} elseif ( $rel == 3 ) {
return $result;
} else {
return $result;
}
}
$pr = HELLO(3);
echo $pr;
It code work aright.
Then I wanted to do one function to process the data and output the result.
Code:
function out( $rel, $result ) {
if ( $rel == 1 ) {
print $result;
} elseif ( $rel == 2 ) {
echo $result;
} elseif ( $rel == 3 ) {
return $result;
} else {
return $result;
}
}
function Hello( $rel ) {
$res = mysqli("SELECT * FROM TABLE");
$result = $res->num_rows;
out( $rel, $result )
}
$pr = HELLO(3);
echo $pr;
But now code not work(not show results on line echo $pr;)...
Tell me please why i have error and how write aright?
P.S.: i not know that need use return before function.
Thanks all for my new knowledge.
You simply forgot to add return to out($rel,$result)
as it is right now, your Hello() function doesn't have return value.
You have not return the value in the second code.
you need to use like this:
return out($rel,$result).
The return is in the second function, second function returns the value to function first, now function first also needs to return, so u need to add return there too.
I am trying to use the following code to 1st check if a user has a certain member level, then if they have a blog on the wp network. If they pass both those checks then a link is echoed, if they dont pass the first if check then another link is echoed. Also though, I am trying to check if they pass the first if but fail the second one then a different link is echoed. Here's the code I have now -
<?php
if(pmpro_hasMembershipLevel(array(2,4))) {
if(current_user_can( 'edit_posts' )) {
global $current_user;
$blogs = get_blogs_of_user( $current_user->id );
if($blogs) {
foreach ( $blogs as $blog ) {
if($blog->userblog_id != 1) {
echo '<li>My Site</li>';
} else {
echo '<li>Register your Site</li>';
}
}
}
}
} else {
echo '<li>UPGRADE</li>';
}
?>
The code above echoes the register link when its suppose to but when the user has a blog, the register link shouldnt show but now it shows next to my site link. Any ideas?
EDIT
Free user sees a UPGRADE link
Premium Users without site see a REGISTER Link ( the membership array of 2,4 are the levels they have to be either one of )
Premium members with a site will see the MY SITE link.
EDIT
I was able to use the print_r and on the page where it's suppose to echo the register link -- Array ( [1] => stdClass Object ( [userblog_id] => 1 [blogname] => mysite.com [domain] => mysite.com [path] => / [site_id] => 1 [siteurl] => https://mysite.com [archived] => 0 [spam] => 0 [deleted] => 0 ) )
Looking at the Wordpress MU documentation, I would guess that the get_blogs_of_user always returns an array, so checking on the value of $blogs exists is always going to return true. In the following code, I suggest replacing the simple check on the existence of a value with a check to determine if the returned value is an array and, if so, whether it has elements or not:
<?php
if (pmpro_hasMembershipLevel(array(2,4))) {
if (current_user_can( 'edit_posts' )) :
global $current_user;
$blogs = get_blogs_of_user( $current_user->id );
/*Check if we got an array back and, if so,
check if it has elements*/
if ( is_array($blogs) && ( count($blogs) > 0 ) ) {
foreach ( $blogs as $blog ) :
if($blog->userblog_id != 1) {
echo '<li><a href="http://' . $blog->domain
. $blog->path
.'wp-admin/">My Site</a></li>';
}
endforeach; // end foreach loop
} else {
echo 'Register your Site';
} // end if $blogs
endif; // endif current_user_can
} else {
?>
<div>UPGRADE</div>
<?php
}
?>
Try this :
<?php if(pmpro_hasMembershipLevel(array(2,4))) {
if(current_user_can( 'edit_posts' )) {
global $current_user;
$blogs = get_blogs_of_user( $current_user->id );
if($blogs) {
foreach ( $blogs as $blog ) {
if($blog->userblog_id != 1) {
echo '<li>My Site</li>';
}
}
} else {
echo 'Register your Site';
}
}
} else { ?>
<div>UPGRADE</div>
<?php } ?>
Give this one a shot. Even if it doesn't work in its current state, it should be easier to see the logic and figure out whats not working properly.
EDIT: Shamelessly stole #JustinPearce's method of checking if the user has a blog from his answer
<?php
global $current_user;
$blogs = get_blogs_of_user( $current_user->id );
// print_r($blogs);
$has_membership_level = pmpro_hasMembershipLevel(array(2,4));
$has_blog = ( current_user_can('edit_posts') && is_array($blogs) && count($blogs) > 0 );
$registerLink = 'Register your Site';
$upgradeLink = '<div>UPGRADE</div>';
function echoBlogLinks($blogs) {
echo '<ul>';
foreach ( $blogs as $blog ) {
if($blog->userblog_id != 1) {
echo '<li>My Site</li>';
}
}
echo '</ul>';
}
if ($has_membership_level) {
if ($has_blog) {
echoBlogLinks($blogs);
} else {
echo $registerLink;
}
} else {
echo $upgradeLink;
}
Below is my example script:
<li><a <?php if ($_GET['page']=='photos' && $_GET['view']!=="projects"||!=="forsale") { echo ("href=\"#\" class=\"active\""); } else { echo ("href=\"/?page=photos\""); } ?>>Photos</a></li>
<li><a <?php if ($_GET['view']=='projects') { echo ("href=\"#\" class=\"active\""); } else { echo ("href=\"/?page=photos&view=projects\""); } ?>>Projects</a></li>
<li><a <?php if ($_GET['view']=='forsale') { echo ("href=\"#\" class=\"active\""); } else { echo ("href=\"/?page=photos&view=forsale\""); } ?>>For Sale</a></li>
I want the PHP to echo the "href="#" class="active" only when it is not on the two pages:
?page=photos&view=forsale
or
?page=photos&view=projects
I've also tried this and it doesnt work:
<li><a <?php if ($_GET['page']=='photos' && ($_GET['view']!=='projects' || $_GET['view']!=='forsale')) { echo ("href=\"#\" class=\"active\""); } else { echo ("href=\"/?page=photos\""); } ?>>Photos</a></li>
You can't do:
if ($var !== 'a' || !== 'b') ...
You have to do:
if ($var !== 'a' || $var !== 'b') ...
If you want to clean that code up I would suggest:
function active_view($content, $url, $view) {
if ($_GET['view'] == $view) {
return link($content, '#', 'active');
} else {
return link($content, $url);
}
}
function active_page_view() {
$args = func_get_args();
$content = array_shift($args);
$url = array_shift($args);
$page = array_shift($args);
if ($_GET['page'] == $page && !in_array($view, $args)) {
return link($content, '#', 'active');
} else {
return link($content, $url);
}
}
function link($content, $href, $class) {
$ret = '<a href="' . $href . '"';
if ($class) {
$ret .= ' class="' . $class . '"';
}
$ret .= '>' . $content . '</a>';
return $ret;
}
and then your code becomes:
<li><?php echo active_page_view('Photos', '/?page=photos', 'photos', 'projects', 'forsale'); ?></li>
<li><?php echo active_view('Projects', '/?page=photos&view=projects', 'projects'); ?></li>
<li><?php echo active_view('For Sale', '/?page=photos&view=forsale', 'project'); ?></li>
The above is illustrative rather than being final and complete. The point I'm trying to get across is you want to get in the habit of using some kind of templating mechanism even if you don't use a templating library (eg Smarty). You rarely want to embed complex logic into what is basically a view. If you use a library of functions (or objects) to create your markup it gives you a lot of control to escape special characters, automatically put in attributes, validate what you're putting in or whatever.
In this example, you probably want to have a data structure that represents your site navigation into which you enter what the current page is and it compares it to all the entries when dynamically constructing the navigation and automatically manipulates the links.
In addition to the problem cletus pointed out you also have an issue with the precedence of the operators. && has a higher precedence than ||. Therefore
if (
$_GET['page']=='photos'
&& $_GET['view']!=="projects"
|| $_GET['view']!=="forsale"
)
is equivalent to
if (
( $_GET['page']=='photos' && $_GET['view']!=="projects" )
|| $_GET['view']!=="forsale"
)
But you obviously want
if (
$_GET['page']=='photos'
&& ( $_GET['view']!=="projects" || $_GET['view']!=="forsale" )
)
Maybe not for two alternatives but if you have more options you might want to consider using !in_array(). e.g.
if (
'photos'=$_GET['page']
&& !in_array($_GET['view'], array("projects","forsale"))
)
<?php $view = $_GET['view'];
if($_GET['page'] == 'photos' && ($view =='projects' || $view == 'forsale'))
{
echo '<li>Photos</li>';
}
else
{
echo '<li>Photos</li>';
} ?>
if( $_GET[ 'page' ] != "photos" && $_GET[ 'view' ] != "forsale") {
...
}
else if( $_GET[ 'page' ] != "photos" && $_GET[ 'view' ] != "projects") {
...
}