• rockstaremperor

    (@rockstaremperor)


    Hi,
    I had been using Code Snippets plugin for codes. Few days back, I tried this plugin and imported all snippets from the former plugin. However, out of 7 snippets, one code snippet is giving me this error “Snippet not activated, the following error was encountered: syntax error, unexpected token “&”
    This is the php code

    function blogger_query_vars_filter( $vars ) {
      $vars[] = "blogger";
      return $vars;
    }
     
    add_filter('query_vars', 'blogger_query_vars_filter');
     
    function blogger_template_redirect() {
      global $wp_query;
      $blogger = $wp_query->query_vars['blogger'];
      if ( isset ( $blogger ) ) {
        wp_redirect( get_wordpress_url ( $blogger ) , 301 );
        exit;
      }
    }
     
    add_action( 'template_redirect', 'blogger_template_redirect' );
     
    function get_wordpress_url($blogger) {
      if ( preg_match('@^(?:https?://)?([^/]+)(.*)@i', $blogger, $url_parts) ) {
        $query = new WP_Query ( 
          array ( "meta_key" => "blogger_permalink", "meta_value" => $url_parts[2] ) );
        if ($query->have_posts()) { 
          $query->the_post();
          $url = get_permalink(); 
        } 
        wp_reset_postdata(); 
      } 
      return $url ? $url : home_url();
    }
    

    Code snippets plugin was working fine with this snippet. I have checked this code on few online code checkers and all said the code is fine. But only this plugin shows the error and snippet is not activated.

    Please suggest.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Mircea Sandu

    (@gripgrip)

    Hi @rockstaremperor,

    Thank you for trying WPCode. I tried copying the code you posted above into WPCode on a fresh install and I am not getting any errors, the snippet activates correctly.

    It’s possible that something else is interfering and when this snippet is being activated it triggers an error due to another snippet or plugin on your site.

    Would it be possible to take a look at the PHP error log maybe that provides more info on the source of the error?

    Thanks

    Thread Starter rockstaremperor

    (@rockstaremperor)

    Would it be possible to take a look at the PHP error log maybe that provides more info on the source of the error?

    Where do I get this PHP error log?

    Plugin Author Mircea Sandu

    (@gripgrip)

    Hi @rockstaremperor,

    You should be able to see that using your hosting account – it really depends on your setup.
    You snippet doesn’t seem to have any syntax errors in it as it is so maybe you can try activating it with the other snippets disabled? That way maybe you can find if there’s a problem somewhere else.

    Thanks

    Thread Starter rockstaremperor

    (@rockstaremperor)

    This is the PHP error I am getting

    PHP Warning: Undefined array key "blogger" in /home/lsudlknf/public_html/wp-content/plugins/insert-headers-and-footers/includes/class-wpcode-snippet-execute.php(260) : eval()'d code on line 10

    Each time I activate the code snippet, it automatically deactivates the code snippet itself.

    When I am using this code snippet in Code Snippets plugin, it too generates the same PHP error but the code snippet functions fine. Only in this plugin, code snippet is disabled automatically.

    Plugin Author Mircea Sandu

    (@gripgrip)

    Hi @rockstaremperor,

    Thank you for the details – it looks like the query_vars check is throwing a warning there – can you please try replacing the code you are using for the blogger_template_redirect function with this:

    function blogger_template_redirect() {
    	global $wp_query;
    	if ( isset ( $wp_query->query_vars['blogger'] ) ) {
    		$blogger = $wp_query->query_vars['blogger'];
    		wp_redirect( get_wordpress_url ( $blogger ) , 301 );
    		exit;
    	}
    }
    Thread Starter rockstaremperor

    (@rockstaremperor)

    I replaced with the above code and I haven’t had the auto disabling code snippet occurring for the last 12 hours. So I guess it worked. Thanks for the help.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Snippet not activated due to syntax error’ is closed to new replies.