Support » Plugins and Hacks » Hacks » How to display information on a page

How to display information on a page

  • Hi. I am new to .php coding. I am trying to create a plugin that will search a database and display the information on a page. I have some of the coding already in multiple other script files. Although, I don’t know where to start on my starting plugin-name.php file. I’m not sure what action hooks to use to make the plugin script run other scripts. I am also unsure of what action hooks to use to make it display on specific pages (only 2 or 3 in the site). Any help? again, i am very new to writing plugins and php coding. Thanks for the help

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz


    Displaying information at a particular spot on a page from a plugin can be tricky or simple, depending on where that spot is. Various themes display different things differently, but there are often common elements you can use to at least have a decent chance of your plugin working on various themes.

    Start by learning which templates output the content you are interested in adding to with the default theme, or your chosen theme if the plugin is for yourself. Many themes will work off the same default theme organization. It can be difficult to identify which template is actually generating the output. I’ve resorted to putting html comments at the top of each template identifying it. The comments stand out nicely when one views the browser’s page source of the output.

    Once you’ve found the right template, view its PHP code and see which WP functions are being called to output information near where you want your information to appear. Locate the source code of these functions and look for any calls to apply_filters() and do_actions(). These are where the hooks are initiated that you may want to hook on to. Look at what the function passes to any hooks and what it does with the returned values, if anything. If you are developing for your own site only, things are much easier. Start a child theme and simply call you own functions on the child template where ever you want.

    Based on this research, you can hopefully develop a scheme to display your information where and how you want. To actually begin a plugin, use FTP to create a plugin folder in wp-content/plugins/. In this folder, place you new main plugin php file. Beside containing your code and includes to other files, this file needs to have a particularly formatted comment at the top. After that, you can do pretty much whatever you need to do.

    That should get you started, have fun!

    Thanks for the reply. I am using the Graphene theme. What i am basicly doing is making a directory type plugin. Users will be able to search a database by choosing selecting information from a search bar via javascript. once they click submit, a php file will execute, collect the specific data from the database and send it back to the javascript to display the information on a page. Im using the same idea as this guy here: http://teamtutorials.com/web-development-tutorials/pulling-drop-down-values-from-a-database-using-javascript-and-php
    Just as an example, as i a a newbie to all of this, what would an example of an HTML comment look like?
    I know that i can use the echo tag to display information via php. Although it would be very useful information to know how to display java and html through php considering the plug in I am trying to make.
    I have explored many options with this. I originally wasn’t planning on making a plugin. I tried entering some code through the text field on the edit page, page. I tried creating a child theme, but whenever i copy and past the graphene theme, rename it graphene-child, and activate it, the layout is messed up and isnt as it should be. I’v created a custom template, but when doing that, i can’t display javascript, or execute another script without interrupting the loop.php So i just thought it would be easiest to create a plugin because it should be able to execute other scripts in the plugin folder without a problem, correct? I think this would be the most efficient way to do it, correct?
    But again, back to the main question for this reply, how would i add an HTML comment and allow it to be displayed in a php script?

    Moderator bcworkz


    The comment I was referring to for identifying templates would not display on the normal HTML browser view. You would need to view the HTML source code in order to see these comments. The comment would be something like this:
    <!-- Graphene template content-single.php -->

    Be sure the comment is in the HTML portion and not inside a <?php ?> block. This has nothing to do with your task of displaying information based on user input, it is just a technique for allowing yourself to discover when various templates are brought into play by the theme. It’s just an educational gimmick.

    You should be able to create a child theme without it messing up the layout. If your child theme CSS file contains nothing but the required child theme comment block and an import line for the parent theme CSS file, then you have not done anything to alter anything about the theme, it should appear identically to its parent.

    Similarly, simply creating a template will not interrupt the loop unless you’ve done something to interrupt the loop. These approaches are valid when implemented correctly. However, from your description, I think a plugin is a logical approach. Not because it’s easier, the other approaches should not be any more or less “easy”. You can mess things up just as easily from a plugin. Plugins can actually be more difficult because they have limited ways to control the output. Child themes can manipulate output any way needed with few restrictions.

    I hope I’ve cleared up any confusion. Believe it or not, I am actually confused easily sometimes as well. I’m happy to answer any more of your questions, but could you restrict them all to one thread or the other please? I quickly forget what I said between one thread and another 🙂

    Yeah. Im sorry. I didn’t realize that you were the same person. Lets move this over to this thread here

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to display information on a page’ is closed to new replies.
Skip to toolbar