0

I have been struggling to code a function, which echoes a code, when the user role is 'administrator' or is um_verified'

Honestly I don't know much of what it means, but still, I want to solve this problem. This function picks up the user id for the given WordPress post, then gets the author data, using the Author Data it finds out the user role, and if the user role matched the if clause, it echoes it, the whole functions run after content.

My implementation of the code which is given below gives the correct result, basically works, but it shows an error of "Trying to get property 'roles' of non-object" basically which means that the property should be an object.

How can I solve the error, basically I know something is wrong because it piles up about 5mb of errors in just a matter of a few hours. Please help me correct the code, and also, please explain what does an object means in PHP.

function Milyin_After_Content($content) { 
        if( is_single() ):
        ob_start();

     $user_meta=get_userdata(get_the_author_meta('ID'));
 $user_role=$user_meta->roles;
if (in_array("administrator", $user_role) | in_array("um_verified", $user_role)){
    echo '<svg height="12pt" viewBox="0 0 512 512" width="12pt" xmlns="http://www.w3.org/2000/svg"><path d="m256 0c-141.164062 0-256 114.835938-256 256s114.835938 256 256 256 256-114.835938 256-256-114.835938-256-256-256zm0 0" fill="#1f1fff"/><path d="m385.75 201.75-138.667969 138.664062c-4.160156 4.160157-9.621093 6.253907-15.082031 6.253907s-10.921875-2.09375-15.082031-6.253907l-69.332031-69.332031c-8.34375-8.339843-8.34375-21.824219 0-30.164062 8.339843-8.34375 21.820312-8.34375 30.164062 0l54.25 54.25 123.585938-123.582031c8.339843-8.34375 21.820312-8.34375 30.164062 0 8.339844 8.339843 8.339844 21.820312 0 30.164062zm0 0" fill="#fff"/></svg>  
    ';}
        $code_content_2 = ob_get_clean();
        $content .= $code_content_2;
        endif;
        return $content;
    }

add_filter('the_content', 'Milyin_After_Content_2', 10);

This is the code, and the code that I echo is just the normal HTML, basically the SVG tag, so there should not be a problem with it. If there exists a better approach or a built-in Wordpress function to check for an array of user roles, then that would be even better, but as this was the only method I found, please help me debug it, and improve it...

  • I don't get that error using your code (with a couple of fixes: two pipes || instead of one, and adding your current function name instead of the _2 function name). Is it possible you still have two functions and you forgot to hook the correct one, so the one you're not intending to use is still in use, and this version that does work without errors just isn't running? – WebElaine Oct 30 at 13:56
  • Well ya that 2 was a typo, and i shall surely use || – Aditya Agarwal Oct 30 at 14:21
0

An object in PHP is a data type that you can read more data types here

It's good practice to check variable types before using them. In your case, you are assuming $user_meta is an object. Since you are filtering the_content(), it might be called some place else and returning that error.

Here is your code updated with is_object() to check on that variable before you use it:

function Milyin_After_Content($content) { 
  if( is_single() ):
    ob_start();

    $user_meta=get_userdata(get_the_author_meta('ID'));

    // NOTE: 
    // First check
    if(is_object($user_meta)){

      // Then set your variable here
      $user_role=$user_meta->roles;

      if (in_array("administrator", $user_role) || in_array("um_verified", $user_role)){
        echo '<svg height="12pt" viewBox="0 0 512 512" width="12pt" xmlns="http://www.w3.org/2000/svg"><path d="m256 0c-141.164062 0-256 114.835938-256 256s114.835938 256 256 256 256-114.835938 256-256-114.835938-256-256-256zm0 0" fill="#1f1fff"/><path d="m385.75 201.75-138.667969 138.664062c-4.160156 4.160157-9.621093 6.253907-15.082031 6.253907s-10.921875-2.09375-15.082031-6.253907l-69.332031-69.332031c-8.34375-8.339843-8.34375-21.824219 0-30.164062 8.339843-8.34375 21.820312-8.34375 30.164062 0l54.25 54.25 123.585938-123.582031c8.339843-8.34375 21.820312-8.34375 30.164062 0 8.339844 8.339843 8.339844 21.820312 0 30.164062zm0 0" fill="#fff"/></svg>';
      }
      $code_content_2 = ob_get_clean();
      $content .= $code_content_2;
    }

  endif;

  return $content;
}

add_filter('the_content', 'Milyin_After_Content_2', 10);

(Also note @WebElaine's comment above, you should be using two pipes in that if() statement.)

Hope that helps!

  • Hi there, I did as suggested in the comments, and further on tried your code. Your code runs, but another problem has emerged, it says undefined variable user_role in the wordpress debug log... What can we do to solve it... – Aditya Agarwal Oct 30 at 15:19
  • 1
    The second if statement depends on $user_role existing - and it only exists if $user_meta is an object. So, moving the second if statement inside the first will solve this issue. – WebElaine Oct 30 at 17:04
  • @AdityaAgarwal, I've update the above snippet to fix that issue, give that a try? WebElaine pointed out an oversight I made. – ChristopherJones Oct 30 at 19:58
  • Yep, WebElaine and ChristopherJones, thank you to both for helping me, currently the code runs and there are no errors reported, just in case i find some problem in it, i shall contact you guys again. Thanks for support anyway.. :-) – Aditya Agarwal Oct 31 at 2:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.