how to not show plugin in admin area

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:

Read More
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?

Related posts

Leave a Reply

2 comments

  1. 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.

  2. 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.