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=""><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 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/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...
Share Improve this question edited Oct 30, 2019 at 15:13 Gufran Hasan 6918 silver badges20 bronze badges asked Oct 30, 2019 at 13:15 Aditya AgarwalAditya Agarwal 2764 silver badges22 bronze badges 2- 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 Commented Oct 30, 2019 at 13:56
- Well ya that 2 was a typo, and i shall surely use || – Aditya Agarwal Commented Oct 30, 2019 at 14:21
1 Answer
Reset to default 1An 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/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!