WordPress.org

Ready to get started?Download WordPress

Forums

Newbie: Question on WordPress coding best practices (9 posts)

  1. rkfoster
    Member
    Posted 2 years ago #

    Greetings,

    I've done a few custom themes for WordPress but I'm looking to get further into specialized themes and plugins. Please point me to specific pages in the Codex if that is easier than answering the questions below. Thanks.

    One of the first things I've come up against is where the best place is in a theme or plugin to include javascript functions. With a typical web site I would add the ubiquitous "js" directory at the web root and put everything there. Then reference them in the head section. With a WordPress theme there doesn't seem to be such a directory in most theme directories. One advice was to just but them in header.php but I prefer not to have lengthy javascripts in the header. Should I just have a scripts.js file for the theme in the theme directory and put all the javascript functions there?

    As for plugins, my main question is how to provide a data field in the WP control panel, for either a theme or a plugin, that a user can enter data into, likely some html embed/object code. Many of the plugins provide this for settings and such but I'm confused how one stores this into the database.

    Also, there is mention that the footer can be used to "hook" includes into a web page. I'm not sure how these "hooks" work.

    Any direction or help would be much appreciated.
    Thanks,
    Bob.

  2. David Gwyer
    Member
    Posted 2 years ago #

    Hi Bob,

    When adding JS files via a theme or Plugin best practices dictate that you should always enqueue them. You see here for more information to get you started:

    http://planetozh.com/blog/2008/04/how-to-load-javascript-with-your-wordpress-plugin/
    http://codex.wordpress.org/Function_Reference/wp_enqueue_script

    There is a hook in the page footer that you can use to include extra code in your page (without having to alter the theme footer file manually). For more info:

    http://codex.wordpress.org/Plugin_API/Action_Reference/wp_footer

  3. rkfoster
    Member
    Posted 2 years ago #

    Thanks much for the information. For some reason I missed the notification of your post, but thanks I'll followup on those links.

    - Bob.

  4. chaseman
    Member
    Posted 2 years ago #

    For the data field you can use:

    add_option, get_option, update_option. WordPress has its own options table where you can save and retrieve data. This is how admin panels for the themes specifically are made.

  5. rkfoster
    Member
    Posted 2 years ago #

    Well, I got those javascripts integrated okay.

    Thanks @chaseman I will look into the add_option, get_option, update_option functions.

    The fields I need to implement are specifically for the custom theme I'm putting together. They need to contain photo slideshow embed code for picasa, flickr, or smugmug. The embeds are pretty standard so I may just provide fields for gallery ID. But, if I need to allow a field to contain the entire embed html, isn't there a "cleaner" function in WordPress somewhere? For example, I would like to convert all ampersands into character entities after a user enters the code text into the field.

    Sorry, even though I think the WordPress Codex is excellent, I find it hard to wade through to find what I'm looking for.

  6. chaseman
    Member
    Posted 2 years ago #

    WordPress itself and the Codex can be like a jungle, it takes time until you're able to guide yourself through and know all the corners.

    Yesterday I spent 3 hours trying to make a CSS file in the admin panel work, I went through all possible methods which are documented in the Codex, plus tutorials I found online, until I realized that I did a stupid mistake.

    I think you can find most of the things in the Codex, you just have to get a feel for what you're looking for. Think about what you need and then try to guess how that WordPress specific function may be called and then look for it, I use CTRL+F a lot or the Wordpess' search.

    Though it's a lot of fun to work with WordPress, and I want to become a WordPress hacker! =D

    To answer you question, you could embed the code into your theme code and then provide the user an input field in the admin panel.

    Once the user has entered the gallery id you can then do a conditional check, if it's true, the embedded code will be printed.

    if (isset($gallery_id)):
    // gallery code here
    endif;

    You can retrieve the gallery_id like this:

    $gallery_id = get_option ('gallery_id');

    Which fetches it out of the database field.

    Hope that helped!

  7. rkfoster
    Member
    Posted 2 years ago #

    @chaseman - Thanks for the help.

    I found a section on Data Validation:
    http://codex.wordpress.org/Data_Validation

    but am still thinking through when/if I need to apply this to a control panel field that would contain XHTML code and then be inserted into the database using update_option.

    Still working on how I get my "panel" to show up somewhere in the control panel, I guess as an options panel for the theme.

    - Bob.

  8. esmi
    Theme Diva & Forum Moderator
    Posted 2 years ago #

  9. rkfoster
    Member
    Posted 2 years ago #

    @esmi

    Thanks much, that is exactly what I needed!

    - Bob.

Topic Closed

This topic has been closed to new replies.

About this Topic