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
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!
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.
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?
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 🙂
- The topic ‘How to display information on a page’ is closed to new replies.