0
<?php echo do_shortcode( '[buddypress id="49"]' ); ?> 
<?php echo do_shortcode( '[buddypress id="50"]' ); ?> 
<?php echo do_shortcode( '[buddypress id="51"]' ); ?> 
<?php echo do_shortcode( '[buddypress id="52"]' ); ?> 

I used wordpress and I want to use many PHP codes:

I need a JavaScript code to make a random system to appear only one code each time I visit my site from this list.

I tried to use code from Stack Overflow but this code make random and choose one code only yes but it causes many problems because the code load all codes and choose only 1 to appear and the others code make their hide but if I view my site page source I found all codes.

Because that I need a code to choose only one code to appear in my site and in page source and never loaded the other codes.

And also I need make other random to appear the code or not, like 50% appear and make a random system and choose one code to appear and 50% nothing happens.

3
  • Looks like the needs more of a PHP solution; rather than doing multiple do_shortcode calls, you could do a single one in the php and change the parameter being sent in to it. Commented Jun 8, 2016 at 20:30
  • Java is not the same as JavaScript Commented Jun 8, 2016 at 20:32
  • In either JS or PHP there are lots of ways to generate a random number, both with or without uniform distribution, but it really depends on what you need to represent with this "code" as you call it. I don't understand what you are trying to achieve here. Commented Jun 9, 2016 at 0:14

1 Answer 1

1

Well, if you don't want your code to be visible to users, randomize your page server-side. Use http://php.net/manual/en/function.rand.php (syntax: rand(min,max)) to get random content on every page reload. Example:

<?php 
if (rand(0, 1) == 0) { // 50% to appear
  echo do_shortcode( '[buddypress id="' . rand(49, 52) . '"]' );
}
?>

If you need to do this with JavaScript code, put the PHP code above in new file, for example called randomcode.php and then use ajax to fetch it. Example code (using jQuery):

$.ajax({url: "randomcode.php", success: function (result) {
    $("#div1").html(result);
}});

Keep in mind that if you don't use ajax, when user opens the page, it gets cached and may not be redownloaded on page refresh. Try adding <meta http-equiv="Cache-control" content="no-cache"> to <head> of your page or search the web for other ways to disable caching. Please note disabling cache on all subpages will decrease your page position in search engines.

If you are going to use ajax, add following line at the second line of randomcode.php:

header("Cache-Control: no-cache, must-revalidate");

EDIT due to comment: if your id='s are not going to be the next numbers, use following code:

<?php 
if (rand(0, 1) == 0) { // 50% to appear
  $id = 0;
  switch (rand(1, 4)) { // if you are going to use 4 unique id's, let's say: 49, 51, 52, 79
    case 1:
      $id = 49; break;
    case 2:
      $id = 51; break;
    case 3:
      $id = 52; break;
    case 4:
      $id = 79; break;
  }
  echo do_shortcode( '[buddypress id="' . $id . '"]' );
}
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Thaaaaaaaaaaank you, You are perfect :) I used php and it's work fiiiiiiine thanks, But please in id="' . rand(49, 52) . '" Can change it to don't make random from 49 to 52 ? i jsut need to choose between 49 or 52 not random from 49 to 52 because maybe i don't have id 50 are you understand ? And thanks again :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.