Can't add language translation support to Wordpress plugin - wordpress

I am trying to add language/translation support for my Wordpress plugin using POEdit software to create .po files, but the code isn't working and there are no screen error printing out.
My plugin is located in /plugins/site-status/ and the languages directory is located as /plugins/site-status/languages/. In /languages/ directory all .po files have names such as site-status-en_US.po (the unique identifier is called site-status)
Here is the language support code:
function status_language_init() {
load_plugin_textdomain( 'site-status', false, 'site-status/languages/' );
}
add_action('init', 'status_language_init');
Here is the test code for outputing the default/translated text:
echo _x( 'test', 'site-status' );
I very much look forward for your help!
Thanks in advance and with best wishes,
WHOAMi

To debug this, check the return value of load_plugin_textdomain(). And don’t rely on a directory name of your plugin. The user can change it.
$path = basename( dirname( __FILE__ ) ) . '/languages';
$lang_loaded = load_plugin_textdomain( 'site-status', FALSE, $path );
// die harder!
! $lang_loaded and die( $path . ' not found' );

Related

How do I upload PowerPoint slideshow files in Wordpress?

I have some PowerPoint slideshow files, .ppsx, with mime-type application/vnd.openxmlformats-officedocument.presentationml.slideshow, that I want to upload to WordPress. However, when I try to upload it to the media browser, I get the error "Sorry, this file type is not permitted for security reasons.".
This is despite the fact that .ppsx files are in the list of allowed file types and mimetypes.
When you upload a file, WordPress does some security checks on the file in the wp_check_filetype_and_ext function in wp-include/functions.php:2503. Part of these checks is to validate the given mimetype of the file with the mimetype that PHP detects, using the PHP function finfo_file().
However, finfo_file() isn't always accurate, and its results are often OS dependent. In the specific case of .ppsx files, finfo_file() can read the mimetype as application/vnd.openxmlformats-officedocument.presentationml.presentation. WordPress sees this as a potential security risk because it doesn't match the given mimetype for that file extension and shuts down the upload.
wp_check_filetype_and_ext() also has a filter, and we can use this to our advantage:
function my_check_filetype_and_ext( $info, $file, $filename, $mimes, $real_mime )
{
if ( empty( $check['ext'] ) && empty( $check['type'] ) )
{
$secondaryMimetypes = ['ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation'];
// Run another check, but only for our secondary mime and not on core mime types.
remove_filter( 'wp_check_filetype_and_ext', 'my_check_filetype_and_ext', 99, 5 );
$info = wp_check_filetype_and_ext( $file, $filename, $secondaryMimetypes );
add_filter( 'wp_check_filetype_and_ext', 'my_check_filetype_and_ext', 99, 5 );
}
return $info;
}
add_filter( 'wp_check_filetype_and_ext', 'my_check_filetype_and_ext', 99, 5 );
In vanilla WordPress, there is no way to have multiple mimetypes for a single filetype. The above filter runs the mimetype checks again for a secondary set of filetype/mimetype pairs if it fails the first set of pairs. By allowing .ppsx files with the presentation mimetype, you can now upload .ppsx files!
You need to add some code in your configure.php file to upload any type of
define( 'ALLOW_UNFILTERED_UPLOADS', true );
Add this in your configure.php file and you will be able to upload any file format.
or you can follow this link
Following code work fine for me.
function wcAddCustomFileTypeAndExt( $info, $file, $filename, $mimes, $real_mime )
{
if (strpos($filename, '.ppsx') !== false)
{
$info['ext'] = 'ppsx';
$info['type'] = 'application/vnd.openxmlformats-officedocument.presentationml.slideshow';
}
return $info;
}
add_filter('wp_check_filetype_and_ext','wcAddCustomFileTypeAndExt', 99, 5 );

Python script not found on server

I am working on a Wordpress plugin embedding a Python script. I have uploaded the Python script to my server, and at first I got an error message on the web page, where my script is to be run, saying "permission denied" (I have redirected stderr to stdout to enable me to see the error messages). When I added execution permissions I instead get a "not found" message (done via Filezilla).
I can see the script is on the server in the correct folder, and it was apparently found earlier when the permissions were wrong. How can I get my plugin to find the Python file?
Here is the relevant PHP function in the plugin:
function embed_python( $attributes )
{
$data = shortcode_atts(
[
'file' => 'hello.py'
],
$attributes
);
$handle = popen( __DIR__ . '/' . $data['file'] . ' 2>&1', 'r' );
$read = '';
while ( ! feof( $handle ) )
{
$read .= fread( $handle, 2096 );
}
pclose( $handle );
return $read;
}
Turns out it was as simple as the server I was using (Loopia) needed the Python script in a certain folder to be able to find/run it, as well as several settings changed, see this Swedish page: https://support.loopia.se/wiki/python/.

Load textdomain from global languages directory [Wordpress]

I am creating translations for my plugin.
To fetch .mo files from my-plugin/languages directory , I use the function
//Load translation
function load_plugin_textdomain() {
load_plugin_textdomain( 'my-plugin', FALSE, basename( dirname( __FILE__ ) ) . '/languages/' );
}
add_action( 'plugins_loaded', 'load_plugin_textdomain' );
How can i set the location to wp-content/languages
Setting the location to ABSPATH.'/wp-content/languages' doesn't work.
I'm aware you've accepted your own answer, but although it will work it's against WordPress standards.
The load_plugin_textdomain will load from the global languages directory without modification and should be used for bootstrapping plugin translations. The path you specify as the third argument is a fallback to be used in case the global language file is not installed.
To clarify how it works: WordPress will look in the global languages directory strictly at <WP_LANG_DIR>/plugins/my-plugin-<locale>.mo. So as long your text domain and locale code are correct, it will be loaded.
If it isn't found, WordPress will look at the path you've specified which must be relative to the wp-content/plugins directory.
Historically (prior to WP 4.6) the third argument was loaded first, but WordPress decided to swap the order, favouring community translations (globally installed) over author-provided translations (shipped with plugin ).
If anybody else is looking for the anwser , Here it is
function load_txtdomain() {
$locale = apply_filters( 'plugin_locale', determine_locale(), 'my-plugin' );
load_textdomain( 'my-plugin', WP_LANG_DIR . '/my-plugin-' . $locale . '.mo' );
}
add_action('plugins_loaded','load_txtdomain');

Customize upload folder for sites

I´ve just installed a wordpress multisite installation (version 4.2.1) with mapped domains.
For each site the url for an uploaded image which is shown in the browser is http://URL/wp-content/uploads/sites/SITENUMBER/DATE/... and the folder is /path/to/wordpress/wp-content/uploads/sites/SITENUMBER/DATE/....
But I want to use the local path /path/to/wordpress/wp-content/uploads/sites/SITENUMBER/... and want to see in the browser http://URL/wp-content/uploads/DATE/... (like in a single wordpress site).
I´ve read a much about that and I think I know that the option "UPLOAD URL PATH" normally should rewrite this, doesn´t it? But for me it doesn´t work. How could I customize wordpress to fit it?
Update: I´ve found out that in functions.php are define that multisites will get a rewrite of the URL:
// If multisite (and if not the main site in a post-MU network)
if ( is_multisite() && ! ( is_main_network() && is_main_site() && defined( 'MULTISITE' ) ) ) {
if ( ! get_site_option( 'ms_files_rewriting' ) ) {
/*
* If ms-files rewriting is disabled (networks created post-3.5), it is fairly
* straightforward: Append sites/%d if we're not on the main site (for post-MU
* networks). (The extra directory prevents a four-digit ID from conflicting with
* a year-based directory for the main site. But if a MU-era network has disabled
* ms-files rewriting manually, they don't need the extra directory, as they never
* had wp-content/uploads for the main site.)
*/
if ( defined( 'MULTISITE' ) )
$ms_dir = '/sites/' . get_current_blog_id();
else
$ms_dir = '/' . get_current_blog_id();
$dir .= $ms_dir;
$url .= $ms_dir;
...
}}
But I didn´t know how I could change this to get the needed behavior.
Yes you can change your upload directory in WordPress using below function.
add_filter( 'upload_dir', 'upload_dir_filter' );
function upload_dir_filter( $upload ) {
$dir = $_SERVER['DOCUMENT_ROOT'];
$upload_dir = $dir['basedir'] . '/uploads/date';
$upload_url = $dir['baseurl'] . '/uploads/date';
wp_mkdir_p( $upload_dir ); //WordPress will check if the dir exists and can write to it.
$upload['path'] = $upload_dir;
$upload['url'] = $upload_url;
return $upload;
}

Wordpress get plugin directory

Is there any function that would return the full path of my plugin in WordPress?
Example is
path/wp-contents/plugins/myplugin
I have tried plugin_dir_path(__FILE__) but returns the current dir.
I would suggest to use a WordPress internal constant to solve this case:
$my_plugin = WP_PLUGIN_DIR . '/my-plugin';
if ( is_dir( $my_plugin ) ) {
// plugin directory found!
}
Alternative
The other valid alternative is to compute the path from the URL which is more complex/confusing. I would not use this code:
$plugins_url = plugins_url();
$base_url = get_option( 'siteurl' );
$plugins_dir = str_replace( $base_url, ABSPATH, $plugins_url );
// Now $plugins_dir is same as the WP_PLUGIN_DIR constant.
$my_plugin = $plugins_dir . '/my-plugin';
My opinion in this case is: Use the constant WP_PLUGIN_DIR
Yeah as per description of plugin_dir_path it will give you current plugin file path. But as per what you asking here you can do something like below unfortunately no direct way,
$plugin_dir = ABSPATH . 'wp-content/plugins/plugin-folder/';
Edit: 18-09-2021
The best practice is to use with latest version is WP_PLUGIN_DIR as follow:
$plugin_dir = WP_PLUGIN_DIR . '/plugin-folder';
Code For Plugin Root Path
$dir = plugin_dir_path( __FILE__ );
// Example: /home/user/var/www/wordpress/wp-content/plugins/my-plugin/
Code for plugin path
echo WP_PLUGIN_DIR.'/plugin-name';
plugins_dir_path() is a misnomer.
It will always return the path to the current file.
Link to the codex:
https://developer.wordpress.org/reference/functions/plugin_dir_path/#more-information
It is a wrapper for trailingslashit( dirname( $file ) );.
The “plugin” part of the name is misleading – it can be used for any file, and will not return the directory of a plugin unless you call it within a file in the plugin’s base directory.
It is actually synonym for the trailingslashit() function.
IF the current file is in the plugins directory, then yes, the function returns the path of the current file.
However, if you call this function from a file inside any other directory, then current file is not in the plugins directory, and thus it will does NOT return the path to the plugins directory. It does always return the path to the current file, but without a trailing slash.
Most of the answers here are incorrect, or are only "sometimes" correct. Those answers will only work as they say IF your file happens to already be located in the plugins directory! In all other cases they will give you a misleading result: the path to your current file.
It is more likely that your file is in a *subdirectory *of the plugins folder.
If this is the case, this codex shows you how to create a URL to the current file:
https://codex.wordpress.org/Function_Reference/plugins_url
If you are using the plugins_url() function in a file that is nested inside a subdirectory of your plugin directory, you should use PHP's dirname() function:
<?php
echo '<img src="' . plugins_url( 'images/wordpress.png', dirname(__FILE__) ) . '" > ';
?>
You would then need to remove your file name to get the path.
Or use ``
ANSWER:
These solutions solutions are independant of where the file of your calling function is located. If your file is located in the plugins folder, or a subdirectory of it, then the above options would work. Otherwise, you'll need to resort to something along the lines of:
WP_PLUGIN_DIR
WordPress does have a constant defined, for the Plugins' folder:
Codex: https://codex.wordpress.org/Determining_Plugin_and_Content_Directories
Constants
WordPress makes use of the following constants when determining the path to the content and plugin directories. These should not be used directly by plugins or themes, but are listed here for completeness.
WP_CONTENT_DIR // no trailing slash, full paths only
WP_CONTENT_URL // full url
WP_PLUGIN_DIR // full path, no trailing slash
WP_PLUGIN_URL // full url, no trailing slash
// Available per default in MS, not set in single site install
// Can be used in single site installs (as usual: at your own risk)
UPLOADS // (If set, uploads folder, relative to ABSPATH) (for e.g.: /wp-content/uploads)
Or, If you can guarantee that the plugins folder is located in the normal place for a WordPress install, i_a's answer above answer above will work, no matter what directory your file (that you want to call this function from) is in.
Please see his more complete post in this thread, but so as to not have a "link only answer", I'll include here that it Essentially, it boils down to using the following (and turning it into a function):
$plugins_root = WP_CONTENT_DIR . '/plugins';
Or M07's post, also in this thread, here: https://stackoverflow.com/a/28525164/5411817
Kinda late to this party, but just in case some else stumbles upon this.
plugin_dir_path(__FILE__) will always return the current path (where the file calling it is located).
If you want the root, use the code below:
plugin_dir_path( dirname( __FILE__ ) )
You can then define a constant:
define( 'YOUR_PLUGIN_DIR', plugin_dir_path( dirname( __FILE__ ) ) );
require_once YOUR_PLUGIN_DIR . 'includes/admin-page.php'
require_once YOUR_PLUGIN_DIR . 'admin/classes.php'
You can define a constant into your main PHP file. It should be at the root of your plugin folder.
The file should be here : .../wp-content/plugins/plugin-folder/my-plugin.php
You can add into the file this line.
define( 'MYPLUGIN__PLUGIN_DIR_PATH', plugins_url( '', __FILE__ ) );
After you can use your new constant anywhere in your plugin.
public function Test()
{
$folder2 = MYPLUGIN__PLUGIN_DIR_PATH . '/folder1/folder2/';
// $folder2 = .../wp-content/plugins/plugin-folder/folder1/folder2/
}
I hope it will help someone.
As noted on the section Common Usage of Plugins Url function reference page, that's what worked for me:
If you are using the plugins_url() function in a file that is nested inside a subdirectory of your plugin directory, you should use PHP's dirname() function:
echo plugins_url('', dirname(__FILE__) );
The output for this was:
http://domain/app/wp-content/plugins/my-plugin
The file that called the function was my functions.php inside includes, so the complete path to my file was:
http://domain/app/wp-content/plugins/my-plugin/includes/functions.php
I will suggest following code. if you are accessing this function from any subfolder.
plugins_url( 'images/logo.png' , dirname(__FILE__ ));
Unfortunately, most of the answers here seem to forget about one important thing.
In addition to the plugins dir, plugins might be also in the Must-use plugins (mu-plugins) directory
Because of that, we need to check multiple directories.
An example function to do this:
function get_plugin_dir_path($pluginFolderName){
if ( defined( 'WPMU_PLUGIN_DIR' ) && file_exists( trailingslashit( WPMU_PLUGIN_DIR ) . $pluginFolderName ) ) {
return trailingslashit( WPMU_PLUGIN_DIR ) . $pluginFolderName;
} elseif ( defined( 'WP_PLUGIN_DIR' ) && file_exists( trailingslashit( WP_PLUGIN_DIR ) . $pluginFolderName ) ) {
return trailingslashit( WP_PLUGIN_DIR ) . $pluginFolderName;
}
return false;
}
plugin_dir_path( __FILE__ ) will give you plugin path of current file.
this is mean if you call this function like that inside "your_plugin_dir/sub_dir/file.php"
will return "your_plugin_dir/sub_dir/"
if you want to get the ROOT of your plugin directory, just change __FILE__ to __DIR__
plugin_dir_path( __DIR__ )
Here is a solution, when you are not inside the plugin root:
As of now with 4.7.5, WordPress does not have a get_plugins_root() function like there is a get_theme_root() function. This is probably because you really shouldn't ever need to modify plugins from your theme, and the plugins root directory never changes.
However, it can be useful if you need to programmatically affect plug-ins while developing themes.
Simply, as WP does for the theme root:
$plugin_root = WP_CONTENT_DIR . '/plugins';
Or, if you need a function, why not just do it the same way WordPress does it for the theme root?
Just make a copy of the function get_theme_root() from wp-includes/theme.php and paste it into your theme's functions.php file, rename the function to get_plugins_root(), simplify it, and change 'theme' to 'plugins' in the function...
get_plugins_root() {
$plugins_root = WP_CONTENT_DIR . '/plugins';
/**
* Filters the absolute path to the plugins directory.
*
* #since now
*
* #param string $plugins_root Absolute path to plugins directory.
*/
return apply_filters( 'plugins_root', $plugins_root );
}
With the path, you can now add the plug-ins folder name that you wish to affect.
$the_plugin_root = get_plugins_root()."/the-plugin-name/";
My solution has been to use inside plugin_dir_path DIR
plugin_dir_path( __DIR__ ) . $path_inside_plugin_folder;
The above should give you the absolute path to your plugin folder on the server, then you can load your variable with any file within your plugin folder
As mentioned by #tsl143 the function plugins_url() is what you want, along with the __FILE__ magic constant. If you call it from a file inside your plugin folder it will refer to that folder.
Example:
For the directory:
echo plugins_url( '' , __FILE__ );
//outputs: http://www.example.com/wp-content/plugins/my-plugin
For a file or subdirectory in your plugin directory:
echo plugins_url( 'images/logo.png' , __FILE__ );
//outputs: http://www.example.com/wp-content/plugins/my-plugin/images/logo.png
You can gain it by using this method
plugin_dir_url( __DIR__ )
The other solutions either won't work if called from a sub-directory of your plugin or they require that you to hard code your plugin's directory name or you need to define a global variable.
My solution uses a function that can be placed in any directory of your plugin without requiring that you to hard code something.
function get_my_plugin_root_dir()
{
// WP_PLUGIN_DIR will have something like "/home/michael/public_html/wp-content/plugins"
// __DIR__ will have something like "/home/michael/public_html/wp-content/plugins/my-plugin-folder/the-dir-being-called-from
// First, we remove WP's root plugin path from our __DIR__ path.
$path_to_this_files_dir_without_wp_plugin_dir = str_replace(WP_PLUGIN_DIR, '', __DIR__);
// Now, we're left with only our plugin's path, which looks like
// this: "/my-plugin-folder/the-dir-being-called-from"
// Next, we discard any sub-directories that are included with our
// plugin's path and keep the first directory in the path, our
// plugin's directory name.
$plugin_directory_name_only = explode(DIRECTORY_SEPARATOR, $path_to_this_files_dir_without_wp_plugin_dir)[1];
// Now $plugin_directory_name_only has "my-plugin-folder"
// Lastly, we build the plugin's complete root path using
// WP's root plugin directory and our plugin's root dir name.
return WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . $plugin_directory_name_only;
}
As of now, there isn't any function that will return only the full path of your plugin's base directory.
However, the constant WP_CONTENT_DIR is used by WordPress for just about that. But, even though, WordPress uses this constant, they warn us not to use it directly in our plugins or themes. That's because it can cause issues like when users change or rename their plugin's directory from the default one.
Using the plugin_dir_path() function will return the path of the file's directory from where it is called. So, if you call this function from a PHP file that is inside a subfolder of your plugin, it will return the path of that subfolder's directory and not your plugin's base directory.
Answer
Now, as a workaround, we can define a constant in a PHP file that is directly inside the plugin's base directory and then set its value to the result of calling plugin_dir_path( __FILE__ ). This way, we will be able to use that constant anywhere in the plugin to get the full path of the plugin's base directory.
So, open a PHP file located directly in your plugin's base directory. For instance:
../wp-content/plugins/myplugin/myplugin.php
Then, define a constant by setting its value to the result of calling plugin_dir_path() function. For instance:
define( 'MYPLUGIN_PLUGIN_DIR_PATH', plugin_dir_path( __FILE__ ) );
This constant now holds the path of your plugin's root directory. So, now, if you echo that constant from any file located in any directory of your plugin like this:
echo MYPLUGIN_PLUGIN_DIR_PATH;
It will print something like this:
/home/example.com/public_html/wp-content/plugins/myplugin/
This solution wasn't available when the original answer was accepted.
If the current file is two folders down into the plugin path, use this:
$plugin_path = plugin_dir_path(dirname(__FILE__, 2));
$plugin_url = plugin_dir_url(dirname(__FILE__, 2));
Note the number as the second parameter on dirname(), available since PHP v7.0.0.
The technique to use a constant works with this too, this time outside of the root-level plugin.php :
// from pluginname/src/API/Common/Constants.php
define( 'MYPLUGIN_PLUGIN_DIR_PATH', plugin_dir_path(dirname(__FILE__, 3)) );
Note this comment on the PHP Manual site:
Dirname likes to mess with the slashes.
So I normalize the path to avoid double slashes, and slashes that are wrong for the current OS:
$normalized = wp_normalize_path( $plugin_path . '/' . $required_file);
if (file_exists($normalized))
require_once $normalized;
else
die(__('Required file does not exist') . ": $normalized");
As others have stated, using plugin_dir_path(__FILE__) works to get the directory of your plugin if used within the main plugin file (e.g. plugins/myplugin/myplugin.php.
But what if you want to know the plugin directory from another file in your plugin? Well what I like to do is define a constant in my main plugin file like this;
// plugins/myplugin/myplugin.php
define('MYPLUGIN_PATH', plugin_dir_path(__FILE__));
Then in other php files within your plugin, you can use MYPLUGIN_PATH (a constant) anywhere you like, without knowing the directory structure!
rather than take the advice of someone who's not using your server (and continue guessing at it...)
why not just load a so-called "Classic" theme, and see what you get?
You're either going to get a relative, or an absolute path. It's not random. figure it out. if you don't like the output, change the function/ args. If you don't know the function names, Install Visual Studio Code, and one of the several WordPress autocomplete / intellisense plugins. It basically writes the code for you.
e.g. ./wp-content/themes/classic/footer.php
wp_footer();
echo '<br><strong>plugin_dir_url(dirname(__FILE__))</strong><br> '. plugin_dir_url(dirname(__FILE__));
echo '<br><strong>plugins_url()</strong><br> '. plugins_url();
echo '<br><strong>__FILE__</strong><br> ' .__FILE__ .'<br>';
?>
go from there.

Resources