Get uploaded image url

wp_upload_dir() is perfect. It is the only place where you can expect write permissions. Store the attachment ID, not a path. Then you get the image with: wp_get_attachment_url( $attach_id ) Sometimes a user may have deleted the image per media library. To avoid problems with missing files check the return value from wp_get_attachment_url() first. Excerpt … Read more

Programmatically add a custom page/path/url/route to WordPress

The idea is just to programmatically create a path/url in a plugin for a WordPress site (like, “[mysite]/mypath”), and then load an arbitrary html or php file. In case anyone else is looking for something similar, this works for me (in my main plugin function file): register_activation_hook(__FILE__, ‘myplugin_activate’); function myplugin_activate () { create_custom_page(‘mytestpath’); } function … Read more

Wrong path for theme assets

Turns out the problem was with WP_CONTENT_URL, defined inside wp-config.php: define( ‘WP_CONTENT_URL’, ‘http://’ . $_SERVER[‘HTTP_HOST’] . ‘/lab/WordPress-Skeleton/content’ ); It seems that $_SERVER[‘HTTP_HOST’] is returning an incomplete path (only localhost without the folder). Since I’m using version control and using a local-config.php to define some local dev variables (like database credentials), I also placed this on … Read more

Plugin base URL

I think you are looking for plugin_dir_url: $url = plugin_dir_url(__FILE__); $imageurl = $url.’images/someimage.png’; EDIT: Sorry I misread the question… that is only an answer to the linked question. You could check the parent directory recursively until you find the right one: function base_plugin_dir($dirpath) { if (substr(dirname($dir),-7) == ‘plugins’) {return $dirpath;} else {$dirpath = base_plugin_dir($dirpath);} return … Read more

ABSPATH not working! Any idea why?

If you just need that class included, and your script is located in the plugin directory, like /wp-content/plugins/pluginName/script.php, then you can do: require realpath(‘../../../wp-includes/class-phpass.php’);

issues including a file from a plugin directory

The function plugin_dir_path has an misleading name, it doesn’t include a file from plugin directory, it just include a file from the same directory of the file passed as argument. When you call include( plugin_dir_path( __FILE__ ) . ‘test-plugin/needed_file.php’); from a file in theme directory, you are just trying to include a file from theme … Read more

Migrating a File from Plugin to Theme and changing its path → instead create a REST endpoint

I’m not sure why are you trying to print out a PHP file’s path, because it’s not a good practice. But to get the equivalent function for themes, you can use: For parent themes : get_template_directory_uri(); For Child themes: get_stylesheet_uri(); Both are without trailing slash. Important Note Don’t ping a PHP file directly from your … Read more