• Hope this is in the right forum…I’m in the middle of creating a WordPress Plugin more or less for the first time, and have hit a bit of a wall. I’ve read through quite a bit of the Codex, but I’m still thoroughly lost on my next steps – and many of the Codex’s examples don’t seem to work.

    The plugin I’m creating is intended to let users select backgrounds for single post pages based on the tags associated with that post. So a post tagged “horses” can be set up to have a background image of horses, and so forth. I’ve got the settings page set up to create a drop down menu via get_tags already, but haven’t gotten beyond that yet.

    My basic thought process/plan was to have the plugin’s user be able to pick from the different tags on the settings page (via drop down menu) and allow the user to type in the url for the background image. They can then save that tag + background image url to the WP Database.

    Then, at the head of each post (via wp_head?), check to see what tags the post is tagged with, and check to see if any of the tags match those in the database that have images associated with them. If yes, use CSS to change the page’s background; if no, default to the background in use by the theme. (Aka, do nothing)

    Where I’m stuck is:

    1) I’m not sure how to create tables/store this data in the WordPress Database;
    and 2) I’m not sure how to properly handle the code for the header.

    I do know enough PHP to do this “raw” (so to speak), and know how to prevent script injection, but I’m very new to WordPress and so I’m getting bogged down trying to figure out how WordPress handles these things.

    Any help/advice you guys can offer would be greatly appreciated! I can provide my existing code if it’ll assist, though at the moment it’s turned out fairly barebones. ??

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

    (@bcworkz)

    You can store the data in an array, then save the entire array in the options table by using update_option().

    One issue you will run into is backgrounds are normally the realm of themes and CSS. Your plugin will have difficulty dealing with the variety of how things are handled in various themes. One way to get around this is to enqueue javascript that changes the background style for single pages. You can pass PHP values to javascript functions with wp_localize_script().

    You normally localize scripts in the action ‘wp_enqueue_scripts’, but I’m unsure if this is late enough to know if the query is for a single post. Some testing will be required.

    Thread Starter sym_slip

    (@sym_slip)

    Thank for your help, bcworkz! The arrays seems to have done the trick, as far as I can tell. I’m aware of the likelihood of difficulties with themes, but at the moment this plugin is mostly practice/me testing to see what WordPress can do, so I’m not incredibly worried about that (yet). I’ll look into handling the changes via javascript, though ??

    Would an array be the solution for querying/identifying the tags on a post page and activating the changes to the backgrounds? WordPress seems to have the ability to do that (via get_the_tags) but when I try it, the array comes back as null, despite echoing out fine on the admin page.

    Moderator bcworkz

    (@bcworkz)

    Sorry, I don’t fully understand your last question. I’ll give you some information and hopefully with that you can arrive at a workable solution.

    Your problem with get_the_tags() may be that it must be used from within “The Loop”. This is the only reason I can imagine why you are getting a null result. If you are hooking into ‘wp_head’, this is not inside The Loop. You probably need a more generic way to access the tags such as get_the_terms().

    You still need a way to determine what the current post is. This can be determined from the main query object contained in the global $wp_query, specifically the $posts property, which for a single query should only contain a single post, get the ID out of that.

    Now you have the ID, so you can get the tags, with a tag, you can extract the image URL out of your option array. Now you need set this as a background style. You should be able to echo out a <style> block from the ‘wp_head’ callback with no need for javascript.

    This approach should be compatible with almost any theme.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Creating Custom WPDB Tables?’ is closed to new replies.