Sauter la navigation

[Résolu] I need to create a conditional logic using a shortcode that return values array

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem:
I need to create a conditional logic using a shortcode that return values array

Solution:
To return boolean value based on user levels you should add a custom shortcode.

You can find the proposed solution in this case with the following reply:
=> https://toolset.com/forums/topic/i-need-to-create-a-conditional-logic-using-a-shortcode-that-return-values-array/#post-1325079

Relevant Documentation:
=> https://toolset.com/documentation/adding-custom-code/how-to-create-a-custom-shortcode/

This support ticket is created Il y a 5 années et 5 mois. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

Marqué : 

Ce sujet contient 11 réponses, a 2 voix.

Dernière mise à jour par francescoG-3 Il y a 5 années et 5 mois.

Assisté par: Minesh.

Auteur
Publications
#1324289

Tell us what you are trying to do? I'm building a membership site using the Ultimate Membership Pro (UMP) plugin. With this plugin, every user can have more than one membership level.

Now, I need to display some post content by the author post membership level(s).
To get the post author level(s), I have created a shortcode:

add_shortcode( 'livello-autore', 'livello_autore' );
function livello_autore()
{
  $author_id = get_the_author_meta( 'ID' );
$level_array =array();
	$author_levels='';

	$level_array = Ihc_Db::get_user_levels($author_id);

	$i=1;
	foreach ($level_array as $key => $object){
		$author_levels .= $key;
		if ($i < count($level_array)) $author_levels .= ',';
		$i++;
	}

	return $author_levels;
  }

The shortcode return the levels value in array mode (1,2,3,5, ecc... but it could be 1,5,2,3, ecc...).
Now, for a specific post author level, the user can see relative content, for example:
with the silver level, the user can see the post author phone number;
with the gold level, the user can see the post author email address;
with the diamond level, the user can see the post author phone number and email address;
but the post author can have silver and gold level, and the user can see the post author phone number and email address, same as diamond level.

What should I do to set the conditional logic using the array values?

Is there any documentation that you are following? I don't know

Is there a similar example that we can see? I don't know

What is the link to your site? lien caché

#1324991

Hi,
I have tried to use the in_array() function but I think that it don't work fine, I think that the array I want to pass into in_array() function contains object, but I'm not shure...
I have tried to use this custom code, but it not return me any message

add_shortcode( 'nome-autore', 'nome_autore' );
function nome_autore()
{
  $author_levels = do_shortcode("[livello-autore]"); //the shortcode return values as 1,3,5,2,4, ecc...

    if (in_array('2', $author_levels) or in_array('3', $author_levels) or in_array('4', $author_levels) or in_array('5', $author_levels) or in_array('6', $author_levels) or in_array('7', $author_levels) or in_array('8', $author_levels) or in_array('9', $author_levels) or in_array('10', $author_levels))
    {
    return "L'autore è abbonato";
    }
	elseif (in_array('0', $author_levels) or in_array('1', $author_levels))
	{
    return "L'autore NON è abbonato";
	}
}

Also, I have tried to use this custom code, but the only value return is 0

add_shortcode( 'abbonamento', 'abbonamento' );
function abbonamento()
{
  //$author_levels = do_shortcode("[livello-autore]");
  //return $author_levels;
  //$abbonamento='false';
    $author_id = get_the_author_meta( 'ID' );
$level_array =array();
	$author_levels='';

	$level_array = Ihc_Db::get_user_levels($author_id);

	$i=1;
	foreach ($level_array as $key => $object){
		$author_levels .= $key;
		if ($i < count($level_array)) $author_levels .=', ';
       $i++;
            
	if (in_array('2', $author_levels, TRUE) or in_array('3', $author_levels, TRUE) or in_array('4', $author_levels, TRUE) or in_array('5', $author_levels, TRUE) or in_array('6', $author_levels, TRUE) or in_array('7', $author_levels, TRUE) or in_array('8', $author_levels, TRUE) or in_array('9', $author_levels, TRUE) or in_array('10', $author_levels, TRUE))
    {
    $abbonamento='1';
    }
      else {$abbonamento='0';}
   }  

      return $abbonamento;

}

What could be a solution to return the correct value?

#1324995

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Can you please share access details as well as where you added the above code you shared and problem URL.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#1325003

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Can you please tell me where exactly with what field you are using the custom shortcode you created?
=> lien caché

In addition to that, if you added the code to theme's functions.php file you need to send me FTP access details so that I can access the code you added.

I have set the next reply to private which means only you and I have access to it.

#1325065

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

ok - I got it and now I want more information. Do you mean that if the author level is equal to 1 then you want to return 0 and for all other author level you want to return 1?

#1325071

For now if the author level is equal to '', 0 and 1 I want return 0, and for all other author level I want to return 1.
The author levels are added dinamically, and is probably that other levels that I could add, them could return 0.

#1325075

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Ok thanks for all required information.

I've adjusted the shortcode as given under:

add_shortcode( 'abbonamento', 'abbonamento' );
function abbonamento(){
 
    $author_id = get_the_author_meta( 'ID' );
$level_array =array();
	$author_levels=array();

	$level_array = Ihc_Db::get_user_levels($author_id);
    $author_levels =  array_keys($level_array);
    
  if(in_array(1, $author_levels) or in_array(0, $author_levels) ) {
    $abbonamento = 0;
  }else{
      $abbonamento = 1;
  }
       return $abbonamento;

}

And I can see 0 for now on the page:
=> lien caché

Can you please confirm it works for you as expected?

#1325077

Hi, thanks.
No, it not work, because the levels of the post author are 1, 5, 4.
Than, because it has 5 and 4, it need to return 1.

#1325079

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Ok - I've adjusted the code again accordingly as given under:

add_shortcode( 'abbonamento', 'abbonamento' );
function abbonamento(){
 
    $author_id = get_the_author_meta( 'ID' );
$level_array =array();
	$author_levels=array();

	$level_array = Ihc_Db::get_user_levels($author_id);
    $author_levels =  array_keys($level_array);
  
  if (in_array(2, $author_levels) or in_array(3, $author_levels) or in_array(4, $author_levels) or 
      in_array(5, $author_levels) or in_array(6, $author_levels) or in_array(7, $author_levels) or 
      in_array(8, $author_levels) or in_array(9, $author_levels) or in_array(10, $author_levels)) {
    		
    		$abbonamento=1;
    } else {
    		$abbonamento=0;
    }
       return $abbonamento;

}

Now, as you can see it displays and return the 1 as expected. Can you please check:
=> lien caché

#1325095

For now it return 1.
I have do some test and now it work fine!!! Thank you very much!

#1325099

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

your welcome if needed in the future feel free to adjust the code accordingly.

Please feel free to resolve the ticket. Glad to help 🙂

#1325203

My issue is resolved now. Thank you!