• In wordpress we can get parameter form url to content.
    ie www.domainname.com/?ppc=any%20phrase
    to <h1> any phrase </h1>

    
    $variable = $_GET['param_name'];
    
    //Or as you have it
    $ppc = $_GET['ppc'];
    

    Is this possible to make list of allowed phrases, after parameter??

    ie

    
    ppc=super%20hero 
    ppc=easy%20way%20home
    

    for secure reasons, ie using htaccess or any other method ??

    • This topic was modified 5 years, 9 months ago by Jan Dembowski.
Viewing 6 replies - 1 through 6 (of 6 total)
  • A possible solution using WP code options.

    Add to your templates functions.php file.

    
    add_action('parse_query', 'check_querystring_ppc_value_is_valid');
    function check_querystring_ppc_value_is_valid( $wp_query ) {
            // check if we have the ppc query string var
            if (($ppcValue = get_query_var('ppc', 'nope')) != 'nope') {
    
                    // setup the valid choices
                    $validPPCChoices = [
                            'super hero', 'easy way home'
                    ];
    
                    if (in_array(strtolower($ppcValue), $validPPCChoices)) {
    
                            // the ppc value is valid
    
                            // DO SOMETHING
    
                    } else {
    
                            // the ppc value is NOT valid
    
                            // DO SOMETHING
    
                    }
            }
    }
    
    function add_custom_query_vars_filter($vars) {
      $vars[] .= 'ppc';
      return $vars;
    }
    add_filter( 'query_vars', 'add_custom_query_vars_filter' );
    
    Thread Starter ladnie77

    (@ladnie77)

    thank you, it almost works ??
    but I have every phrase four times on page, twice in header twice in content
    I want only one
    I use (in script)
    / the ppc value is valid

    echo get_query_var(‘ppc’);

    and
    (in content) <h1><?php echo “$ppc!”; ?></h1>

    • This reply was modified 5 years, 9 months ago by ladnie77.
    Moderator bcworkz

    (@bcworkz)

    The ‘parse_query’ action fires for every query on the page, even the one for menu items. The ppc query var passed as URL parameter is also available to every query on the page, so it’s presence alone is not enough criteria to only apply it where needed and not apply it where not wanted.

    You need more conditions in your/vrandom’s code to ensure it is only applied where needed. Another possibility would be to make the add_action() call for “parse_query” just before it is needed, perhaps by calling it on the template page. Then have your callback remove itself from the “parse_query” action stack so it no longer executes after it is used once.

    Humm, ok I was thinking you wanted to redirect the page or something based on the ppc value being valid.

    I am not sure what your trying to accomplish. What @bcworkz said might work, but it could be over kill for what you want.

    You could do this if your just wanting to echo out the ppc.

    
    (in content) <h1><?php echo get_query_var(‘ppc’).“!”; ?></h1>
    

    But that doesn’t check to see if its valid.

    Can you explain the process of what your wanting to do?

    Thread Starter ladnie77

    (@ladnie77)

    IDEA is to have parameter DYNAMICALLY FROM url IN h1 in content
    ie. domainname.com?ppc=superhero

    in
    <h1>superhero</h1>

    BUT
    only limited phrases (predefinied)
    ie
    if i write domainname.com?ppc=superhero2 NOTHING HAPPENED

    if I use <h1><?php echo get_query_var(‘ppc’).“!”; ?></h1> , I have in h1 everything I write in url ie superhero2

    Ok, so this is what i understand.

    You want the ppc value to show up on your page in the h1, but only if that value is in the list of valid options, then if the ppc is not value is would be an empty h1 tag.

    If it matters, the display of the ppc value is dependent on what the url contains. So if i pass domainname.com?ppc=super hero, the ppc display value would be “super hero”, but if domainname.com?ppc=Super Hero, then the ppc display value would be “Super Hero”. If you want “Super Hero” to be the display value for “super hero” then you can change the function to set a default display value for valid choices.

    If this is what you want, I think this can be accomplished without using the add_action(‘parse_query’… and in where you want to display the value.

    Delete the original function and the add_action lines, but keep the add_custom_query_vars_filter and its filter line.

    put this function in your functions.php file

    
    function get_ppc_value_if_valid( $default = '',  $beforeText = '', $afterText = '' ) {
    	// check if we have the ppc query string var
    	if (($ppcValue = get_query_var('ppc', 'nope')) != 'nope') {
    
    		// setup the valid choices with the display values
    		$validPPCChoices = [
    			'super hero' => 'Super Hero',
    			'easy way home' => "Easy Way Home"
            ];
    
    		if (in_array(strtolower($ppcValue), $validPPCChoices)) {
    			// the ppc value is valid
                return $beforeText. $validPPCChoices[strtolower($ppcValue)].$afterText;
    		} else {
    			// the ppc value is NOT valid
    		}
    	}
    
    	return $default;
    }
    

    Then in your template used this where you want the ppc value to be displayed,

    
    echo get_ppc_value_if_valid('', '<h1>', '!</h1>');
    

    So with the modified code, for the url, domainname.com?ppc=super hero, it would return <h1>Super Hero!</h1>, and for domainname.com?ppc=super hero2, it would echo nothing. It would not create an empty <h1> tag.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘parameter from URL to content’ is closed to new replies.