Hot answers tagged

34

You can use single_template filter hook. /* Filter the single_template with our custom function*/ add_filter('single_template', 'my_custom_template'); function my_custom_template($single) { global $wp_query, $post; /* Checks for single template by post type */ if ($post->post_type == "POST TYPE NAME"){ if(file_exists(PLUGIN_PATH . ...


30

I think you have to be a little careful because it depends on what you are trying to do. If you are using a child theme get_template_directory(); will still go to the parent theme. However get_stylesheet_directory(); will go to the current theme, child or parent. Also, both these functions return absolute server paths. If you wanted a fully formed URI, ...


22

Okay, I'll take a stab at this. Some limitations I encountered along the way: There are not a lot of filters in subclasses of WP_List_Table, at least no where we need them to be. Due to that lack of filters, we can't really maintain an accurate list of plugin types at the top. We also have to use some awesome (read: dirty) JavaScript hacks to display ...


17

I'm assuming that you put WordPress in your site root and the external directories are also in your site root. The reason this is happening is that .htaccess files follow a hierarchy. Whatever directives are in the top-level .htaccess file flow down and apply to all directories below it. If this is the case, you can do one of several things: Move your ...


15

To make a long story short: get_bloginfo( 'template_directory' ) and get_bloginfo( 'template_url' ) simply return get_template_directory_uri(). So, you can shortcut that second call simply by referring directly to the latter template tag. Refer to source for get_bloginfo(). A few others: 'url' => home_url() 'wpurl' => site_url() 'stylesheet_url' => ...


14

The WordPress PHP Coding Standards Handbook states that filenames should be all lowercase and hyphen-separated. As Squish points out, various routines in the WordPress Template Hierarchy rely on this convention in order to auto-load templates for certain situations. All of that said, you will not break Linux or Apache by using underscores instead of hyphens ...


12

How does Wordpress determine "the main plugin file" ? It is the file in your plugin that contains the plugin header comment /** * Plugin Name: A fresh example * Plugin URI: http://example.com * Description: Foo makes a bar * Version: 2012-06-14.1426 * Author: John Doe * Author URI: http://example.com * TextDomain: your_textdomain * ...


12

This has worked for me in the past for a similar situation : Put this on top of .htaccess ErrorDocument 401 default


10

Note that plugins are all "controllers" by WP standards. It depends on what the plugin is supposed to do, but in all cases I would try to separate the screen output from the PHP code as much as possible. Here's one way to do that easily - first, define a function that loads the template: function my_plugin_load_template(array $_vars){ // you cannot let ...


9

once up a time i did a client project where i had to have archives by first letter. thinking back i'm wondering if shouldn't have just created a hidden taxonomy and then saved the first letter as a term in that taxonomy. anyway, here's what i actually did: /* * Function Create Array of Letters that have post titles (for archive) */ /* When the post ...


9

This depends on how the theme was developed from the beginning. Theoretically, if everything was coded to standards you can rename the folder and nothing bad will happen. When renaming the folder you are at risk of breaking code that asks specifically for files from the theme directory by name. WordPress offers a number of functions to help decouple the ...


9

Updated plugin version available at GitHub. I first saw your Question at [wp-hackers] list, and, after implementing the solution, was about to publish a Q&A for that. Well, it's already here, and has a bounty put on it :) As Daniel Bachhuber points out in the thread: WordPress.com puts themes inside of subdirectories /wp-content/themes/public ...


8

Following Justice Is Cheap lead, I ended adapting the functions from this plugin: http://wordpress.org/extend/plugins/custom-upload-dir/ <?php /* * Change upload directory for PDF files * Only works in WordPress 3.3+ */ add_filter('wp_handle_upload_prefilter', 'wpse47415_pre_upload'); add_filter('wp_handle_upload', 'wpse47415_post_upload'); ...


8

If the plugin structure is: plugins/ some-plugin/ some-plugin.php data/ GeoIP.dat then for PHP 5.3.0+, you could try the magic constant __DIR__ __DIR__ The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname(__FILE__). This directory name does ...


7

You can just register another stylesheet instead of style.css: add_action( 'wp_enqueue_scripts', 'enqueue_theme_css' ); function enqueue_theme_css() { wp_enqueue_style( 'default', get_template_directory_uri() . '/css/default.css' ); } This is, in my opinion, better than using style.css.


6

IMHO, the easiest, most powerful, and most maintainable route is to use an MVC structure, and WP MVC is designed to make writing MVC plugins very easy (I'm a little biased, though...). With WP MVC, you simply make the models, views, and controllers, and everything else is handled behind the scenes for you. Separate controllers and views can be made for the ...


5

Like many here already answered It really depends on what the plugin is supposed to do, but here is my base structure: my-plugin/ admin/ holds all back-end administrative files js/ holds all back-end JavaScript files css/ holds all back-end CSS files images/ holds ...


5

The forward slash works on every operating system supported by PHP. Yes, on Windows too. It is also more readable and easier to type, so there is no need to use the constant. See the tickets #20849 and #15598 for related discussion on Trac.


5

The PHP files in the wp-includes directory will do nothing when accessed directly. They are designed to be include()'d in an existing PHP script, such as on the front-end or in the dashboard. Your Options -Indexes entry in the .htaccess file simply prevents a list of the files in a directory when no index.php is present. It's good practice to use this on a ...


5

You need to reference your WordPress template directory when you register the script. Change this: wp_enqueue_script('my_javascript_file', '/javascripts/app.js', array('jquery')); ...to this: wp_enqueue_script('my_javascript_file', get_template_directory_uri() . '/javascripts/app.js', array('jquery')); Codex reference: get_template_directory_uri()


5

It depends on the plugin. This is my basic structure for nearly every plugin: my-plugin/ inc/ Any additional plugin-specific PHP files go here lib/ Library classes, css, js, and other files that I use with many plugins go here css/ js/ images/ lang/ Translation files my-plugin.php readme.txt ...


5

We're using a mix of all methods. First of all, we're using the Zend Framework 1.11 in our plugins and therefore we had to use a similar structure for the class files because of the autoload mechanic. The structure of our core plugin (which is used by all our plugins as a base) looks similar to this: webeo-core/ css/ images/ js/ languages/ ...


5

You can use: plugin_dir_path(__FILE__); Which as is just a wrapper function anyway for: trailingslashit(dirname(__FILE__));


4

You can use PHP5 RecursiveDirectoryIterator with RecursiveIteratorIterator $directory = '/project_root/wp-content/plugins/your-plugin'; //Your plugin dir $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)); while ($it->valid()) { //Check the file exist if (!$it->isDot()) { //if not parent ".." or current "." ...


4

So you're asking if you install WordPress in a subdirectory, like example.tld/secretwp/ and have the site pointing to example.tld, it will keep "evil" users and bots from knowing about the subdirectory? I don't think it will hold as a security advise, because your site can give away the subdirectory name in many ways. For example from: the use of ...


4

Just to add to the answer by @birgire, check this post on how to hide the fact that you are using Wordpress. I also think that no post basically covers this, but it really help nothing following and applying everything to hide the fact you are using Wordpress and to secure Wordpress, and your code in itself is a security risk. Hackers are stubborn and ...


4

You can do this by adding some code to wp-config.php $request_uri = $_SERVER['REQUEST_URI']; $debug_dirs = array ('/debug-dir1/','/debug-dir2/'); // list of directories to turn on debugging foreach ($debug_dirs as $debug_dir) { if (!strncmp($request_uri,$debug_dir,strlen($debug_dir))) { define('WP_DEBUG', true); } } define('WP_DEBUG', ...


4

I am partial to the following plugin layout, however it usually changes depending on what the plugin requirements are. wp-content/ plugins/ my-plugin/ inc/ Specific files for only this plugin admin/ Files for dealing with administrative tasks lib/ ...


4

A great starting point would be Mike's answer to the question about cloning CrunchBase. You'll want to do something similar with custom post types for entries in your directory. If you want to allow visitors to submit sites, you could perhaps use the TDO Mini Forms plugin to allow visitors to create a new listing, and adjust the settings so that any new ...


4

@kureikain's answer looks great, and it probably works really well in a wide variety of circumstances. But for author URLs specifically, there's a simpler way. Change the author_base, like so: global $wp_rewrite; $wp_rewrite->author_base = "people"; $wp_rewrite->flush_rules(); You should only need to run this once, perhaps on a plugin activation. ...



Only top voted, non community-wiki answers of a minimum length are eligible