Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm just getting started with wordpress development. I'm trying to create a simple plugin that show results from a db in a table. I can do this, but I'm having trouble with the plugin showing at the top of the admin pages. I'm not sure why it does this.

my plugin function:

add_action('init','hello_world');
    function hello_world()
    {

        global $wpdb;
        $query = "Select ID, post_title
                  From $wpdb->posts Limit 0, 10";
        $results = $wpdb->get_results($query, ARRAY_A);

        echo "<div class=\"datagrid\"><table>";
        echo "<thead><tr><th>header</th><th>header</th><th>header</th><th>header</th></tr></thead>";
        echo "<tfoot>
                    <tr>
                    <td colspan=\"4\">
                        <div id=\"paging\">
                            <ul>
                                <li>
                                    <a href=\"#\"><span>Previous</span></a>
                                </li>
                                <li>
                                    <a href=\"#\" class=\"active\"><span>1</span></a>
                                </li>
                                <li>
                                    <a href=\"#\"><span>2</span></a>
                                </li>
                                <li>
                                    <a href=\"#\"><span>3</span></a>
                                </li>
                                <li>
                                    <a href=\"#\"><span>4</span></a>
                                </li>
                                <li>
                                    <a href=\"#\"><span>5</span></a>
                                </li>
                                <li>
                                    <a href=\"#\"><span>Next</span></a>
                                 </li>
                            </ul>
                        </div>
                    </tr>
                </tfoot>";
        echo "<tbody>";
        $i = 0;
        while($i < 10)
        {
            foreach ($results as $item)
            {
                $postID = $item['ID'];
                $postTitle = $item['post_title'];

                echo "<tr class=\"alt\">";
                echo "<td>" .$postID."</td>";
                echo "<td>".$postTitle."</td>";
                echo "</tr>";
                $i++;
            }
        }

        echo "</tbody>";
        echo "</table></div>";
    }

calling in index.php

    <?php
        if(is_admin())
        {

        }
        else
        {
            if(function_exists('hello_world')) {
                echo "<div>";
                hello_world();
                echo "</div>";
            }
        }
    ?>

how can I prevent this from showing in the admin section?

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

To complement @s_ha_dum’s answer: If you just want to offer a function to be used in a theme, make it a custom action.

Sample:

add_action( 'call_hello_world', 'hello_world' );

Now, in a theme, the author can call your function with …

do_action( 'call_hello_world' );

… and the function will print its content only where the author needs it.

This has at least three advantages:

  • You can disable the plugin anytime, and nothing will break. If there is no callback for an action, nothing will happen.
  • You don’t have to check with function_exists(), which is always … ugly.
  • Another plugin can use the same action, or it can replace your callback.

Here is a sample plugin:

<?php
/*
 * Plugin Name: Custom Action
 */

add_action( 'call_hello_world', 'hello_world' );

/**
 * Demo function.
 *
 * Usage:
 * do_action( 'call_hello_world' );
 *
 * @wp-hook call_hello_world
 * @return void
 */
function hello_world()
{
    print '<h1>Hello World!</h1>';
}

// static class method
add_action( 'call_static_method', array ( 'Demo_Class', 'static_method' );


// dynamic class method
$demo = new Demo_Class;
add_action( 'call_dynamic_method', array ( $demo, 'dynamic_method' );

class Demo_Class
{
    public static function static_method()
    {
        print 'I was called statically.';
    }

    public function dynamic_method()
    {
        print 'I was called with an instance.';
    }
}

Install, activate, and use the do_action() in a theme as often as you need it.

share|improve this answer
    
... very good idea. –  s_ha_dum Nov 8 '12 at 22:56
    
so, I replaced it with your suggestion and called the do_action('call_hello_world'); in the index.php page, but the plugin is now not showing. am I missing something? –  user987795 Nov 8 '12 at 22:56
    
@user987795 I have added a sample plugin. Copy it to a separate file, and install that file as a plugin. –  toscho Nov 8 '12 at 23:12
    
thanks for all the help. –  user987795 Nov 9 '12 at 2:41
    
hi @toscho, what if it's an OOP kind of plugin, can this still apply? –  Reigel Jan 28 '13 at 10:45
show 1 more comment

You have hooked your function to init which runs on (I believe) every page load, front or back, and you are echoing data. Not to be harsh but what did you expect to happen?

To solve this, remove the first line-- add_action('init','hello_world'); Your function will then only run when explicitly called from your code in index.php.

share|improve this answer
    
Although the article is incomplete, a link to the Codex is always nice :) - codex.wordpress.org/Plugin_API/Action_Reference/init –  brasofilo Nov 8 '12 at 22:43
    
@brasofilo, yes, you are right. I try to add those links. I forgot. –  s_ha_dum Nov 8 '12 at 22:45
    
thanks, I removed the add_action('init', 'hello_world'); this removed it not only from the admin section, but also the index. Again, I'm just beginning with wordpress dev so this is all new to me. –  user987795 Nov 8 '12 at 22:47
    
@user987795, I assumed that your plugin was properly loading and that this was an excerpt. Maybe that isn't the case. Plugins don't need to be complicated, the hello dolly plugin that ships with WordPress is about as simple as it can gets but you do need a few things, like the right headers. –  s_ha_dum Nov 8 '12 at 22:50
add comment

Your Answer

 
discard

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.