• Hi there! I am using wordpress, and I am unable to use php codes in my pages.
    Somewhere I found that using shortcodes, one can use php code anywhere.
    Please let me know how can I do it.
    I followed many guides, did some work in functions.php too, but still it shows Error 500.

    Please let me know, my php code –

    
    // Add Shortcode
    function custom_shortcode_logincall() {
    
    	<?php   global $current_user;
    	                                $second_website_url = 'https://secondwebsite.com'; // put your second website url
    	                                $user_email = $current_user->user_email;
    	                                $user_login = $current_user->user_login;
    	                                if($user_email != ''){
    	 
    	                                    $email_encoded = rtrim(strtr(base64_encode($user_email), '+/', '-_'), '='); //email encryption
    	                                    $user_login_encoded = rtrim(strtr(base64_encode($user_login), '+/', '-_'), '='); //username encryption
    	                                    echo '<a href="'.$second_website_url.'/sso.php?key='.$email_encoded.'&detail='.$user_login_encoded.'" target="_blank" rel="noopener noreferrer">Link to second website</a>';
    	 
    	                        }?> 
    
    }
    add_shortcode( 'logincall', 'custom_shortcode_logincall' );
    

    Any shortcode name will be appriciated!

    • This topic was modified 5 years, 6 months ago by Jan Dembowski. Reason: Formatting
Viewing 11 replies - 1 through 11 (of 11 total)
  • Thread Starter allroundernaman

    (@allroundernaman)

    Hey! @jandembowski, thanks for modification. Do you have any suggesstions?

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    Shortcodes should not echo content — they return a string that’s printed as part of displaying a post’s content. So, change your echo lines to append content to a string and return that string at the end of the function. Also, you have a <?php there that’s unnecessary. That’s probably what’s causing the 500 error.

    Thread Starter allroundernaman

    (@allroundernaman)

    Many thanks for the advice. Just please correct my syntax, your pleasure!
    Please post the syntax that should be pasted on functions.php

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    What you’re asking me to do is rewrite your bad code for you. I already told you about the two problems with your shortcode’s code. Please give that a try and let me know if it works.

    In addition, you don’t need the ?> after the closing brace.

    Thread Starter allroundernaman

    (@allroundernaman)

    Hey is that bad coded? I am totally a newbie of 14 years old and I have only 1 year of web development experience. Still I understood the error, soon I will tell you whether it worked or not?

    Thread Starter allroundernaman

    (@allroundernaman)

    Yes, I tried.
    1. I removed ?> and <?php from the code.
    2. I replaced echo –> return “CODE HERE”;

    Now when I saved, it returned to Server Error 500. Whole website is not running. Kindly help.

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    Please post your latest code.

    Hello @allroundernaman ,

    Please use this code:

    function custom_shortcode_logincall() {
    
    	ob_start();
    
    	global $current_user;
    	$second_website_url = 'https://secondwebsite.com'; // put your second website url
    	$user_email = $current_user->user_email;
    	$user_login = $current_user->user_login;
    	if($user_email != ''){
    		$email_encoded = rtrim(strtr(base64_encode($user_email), '+/', '-_'), '='); //email encryption
    		$user_login_encoded = rtrim(strtr(base64_encode($user_login), '+/', '-_'), '='); //username encryption
    		?>
    		<a href="<?php echo $second_website_url.'/sso.php?key='.$email_encoded.'&detail='.$user_login_encoded; ?>" target="_blank" rel="noopener noreferrer">Link to second website</a>
    		<?php
    
    	}
    
    	$output_string = ob_get_contents();
    	ob_end_clean();
    	return $output_string;
    
    }
    add_shortcode( 'logincall', 'custom_shortcode_logincall' );
    

    This is working for me.

    Thanks,

    Thread Starter allroundernaman

    (@allroundernaman)

    Ok very much thanks! Just I want to ask that seek in 14th line, you have written *<?php* so is that correct?
    I have not tested yet. Still asking!

    Yes that is correct because on line 12 we close the PHP tag( ?> ) and line 14 we start PHP tag again( <?php ). Line no 13 have HTML tag.

    Thread Starter allroundernaman

    (@allroundernaman)

    Ok. I will try tommorow when my website will be opened.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘I want to make a PHP Shortcode’ is closed to new replies.