• I am writing some code to allow me to have a find and replace function using a shortcode, this bit worked fine, until I tried to add arguments to the shortcode.
    The second I started trying to have arguments/attributes for the URL query and replacement word, it all went to pot.

    The code can be found below, I am new to PHP and have no idea what I have done wrong, this is VERY different to the other languages I have worked with (mostly c#).

    add_filter('query_vars', 'parameter_queryvars' );
    function parameter_queryvars( $qvars )
    {
    $qvars[] = 'productname';
    return $qvars;
    }
    
    //Register Shortcode
    
    function legal_content_function($atts, $content = null) {
    
    	?>
    	<div class="et_pb_row"">
    	<?php
    
    $atts = shortcode_atts(
    		array(
    			'query' => 'productname',
    			'replace' => '=productname=',
    			), $atts);
    	global $wp_query;
    if (isset($wp_query->query_vars[$atts['query']]))
    {
    echo str_replace($atts['replace'], $wp_query->query_vars[$atts['query']], $content);
    }
    else
    {
    echo "<h1>Error</h1>";
    echo "Please use URl query \"?productname=\" to display this page.";
    }
    }
    
    function register_shortcodes(){
    	add_shortcode('legalcontent', 'legal_content_function');
    }
    add_action( 'init', 'register_shortcodes');
    
    function dlp_settings_menu(){
            add_options_page( 'Dynamic Legal Page', 'Dynamic Legal Page Settings', 'manage_options', 'dlp-plugin', 'dlp_init' );
    }
    add_action('admin_menu', 'dlp_settings_menu');
    
    function dlp_init(){
    	echo "<h2>Settings</h2>";
    	echo "<h2>Usage</h2>";
    	echo "<h2>Credits</h2>";
    }
    ?>
  • The topic ‘What am I doing wrong?’ is closed to new replies.