1

I'm getting an error in my site with some PHP code. The error states

Fatal error: Uncaught Error: Using $this when not in object context

function search_shortcode() {
    return '<div class="genesis-404-search">' . get_search_form( false ) . '</div>';
}

// Add shortcode for search form in Genesis Framework
add_shortcode( 'genesis-404-search', array( $this, 'search_shortcode' ) );
Patrick Q
  • 6,373
  • 2
  • 25
  • 34
  • 2
    You are outside a class when using `$this`. Usage of that special variable can only be used within a class. – Qirel Oct 29 '18 at 19:16
  • Is there an alternative way of making this statement then? – Scott Davis Oct 29 '18 at 19:17
  • What that code is? You are asked somthing like this: `function some_WTF_function(){ $this->some_WTF_what = 15; }` Why `my_WTF_function_WTF_nomber_WTF( 28)` does return errors??? – Николай Лубышев Oct 29 '18 at 19:46
  • To be honest I don't know what the code is for. I came in and took over for someone that left the company. I was doing updates from PHP 5.6 to 7.1, then the site was non responsive when I did the update with the error code I partially posted. After some search and turning off the portion of the code with $this I was able to get the site to work with no errors. Though I wanted to try and bring the full code back to working order even though I didnt know what that portion of the code was for. This is why I am seeking assistance, I did find the original Git where the code was created back in 2006 – Scott Davis Oct 29 '18 at 20:00
  • 1
    Not posting this as an answer because I'm not 100% sure it will work, as I'm not a Wordpress user. I believe you can change that last line to `add_shortcode( 'genesis-404-search', 'search_shortcode' );` You can see the documentation for `add_shortcode()` [HERE](https://codex.wordpress.org/Function_Reference/add_shortcode). – Patrick Q Oct 29 '18 at 20:10
  • Is `search_shortcode()` a method of an existing class or a standalone function? – cabrerahector Oct 29 '18 at 21:16
  • From the initial Git and from what was placed in the original code it seems to be a standalone function. – Scott Davis Oct 29 '18 at 21:29

1 Answers1

0

The error Fatal error: Uncaught Error: Using $this when not in object context means that something is referencing a Class/Object with $this, while there's no Class/Object in the current scope.

If the code you posted:

function search_shortcode() {
    return '<div class="genesis-404-search">' . get_search_form( false ) . '</div>';
}

// Add shortcode for search form in Genesis Framework
add_shortcode( 'genesis-404-search', array( $this, 'search_shortcode' ) );

Is wrapped in a Class like so: class Some_Class { /* Your Code */ } then something else is at play.

If not, and it's just native like you posted, replace:

add_shortcode( 'genesis-404-search', array( $this, 'search_shortcode' ) );

with

add_shortcode( 'genesis-404-search', 'search_shortcode' );

WordPress allows you to reference classes with callback functions, which is what the original code is attempting to do. But if there's no class to reference, then you just use the callable function name. I'd also consider replacing the function name to something a bit more unique since you're not in a Class or Namespace, and search_shortcode may result in a naming conflict for being so generic:

function so_53052312_search_shortcode() {
    return '<div class="genesis-404-search">' . get_search_form( false ) . '</div>';
}

// Add shortcode for search form in Genesis Framework
add_shortcode( 'genesis-404-search', 'so_53052312_search_shortcode' );
Xhynk
  • 13,513
  • 8
  • 32
  • 69