WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I tried to use class and object in plugin. I made a simple function with a shortcode but it is executing the function without pasting the shortcode to any page/post.

class example{

function ex_funct(){

$obj = new example();

Why it is showing Hello at admin as well front end header without pasting the code examp_code ?

What is happening here?

share|improve this question
A little side note, don't forget to capitalize the first letter when stating a class. class Example & new Example(); – Bobby Mar 8 '14 at 15:13
@Bobby, that is a convention. It is a common one and one adopted by WordPress Core development, but it is a convention nonetheless. It is not required by PHP. – s_ha_dum Mar 8 '14 at 17:00
I don't know why i got minus vote. @s_ha_dum is helped me which means this is okay. But i don't know who did this and why he did this – rram Mar 8 '14 at 18:10
I don't know what you mean by "@s_ha_dum is helped me". I didn't help. I just made a comment about another user's comment. And even had I made a comment that helped it doesn't make the question OK. I often comment on questions/answers even if they substandard. At any rate, downvotes are anonymous (even to mods), but hover over the down arrow and you will see "This question does not show any research effort...". I am fairly sure that is the reason for the downvote. You have a PHP object syntax problem and two shortcode ones. Research into either of those would have gotten you a long way. – s_ha_dum Mar 8 '14 at 18:25
I don't know what others think but anyway, thanks for your help and explanation about this. I very much appreciate you. Thanks @s_ha_dum – rram Mar 8 '14 at 19:06
up vote 0 down vote accepted

That happens, because you run the function in add_shortcode(). To send is as callback, use:

add_shortcode('examp_code', array ( $obj, 'ex_funct' ) );

Now you have still a problem: you create output in your shortcode callback. Never do that, you have to return a string.

Change it to:

function ex_funct(){
      return '<h3>Hello</h3>';
share|improve this answer
Thanks. +1 any other different way to hook functions with object? just want to know is there some other way exists:) – rram Mar 8 '14 at 15:07
@rram No, that’s the only way. – toscho Mar 8 '14 at 15:09

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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