Wordpress - Plugin translation not working - wordpress

My plugin name is eventcal.Textdomain is eventcal. I have a language pack in /languages/ folder - eventcal-pt_BR.po and eventcal-pt_BR.mo. I have added plugin textdomain function
public static function eventcalLoadTextdomain() {
load_plugin_textdomain( 'eventcal', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}
// in __constructor()
add_action( 'init', array(self::getInstance(), 'eventcalLoadTextdomain' ));
but its not working when i am changing my site language to portugese( Brazil )
Nothing is changing.everything is in english. How can i check plugin textdomain is working or not??
I also tried get_plugin_data( __FILE__ ); but not working

Related

wordpress custom plugin translation file not working

I have a plugin with name "keydesign-addon" and I created file translation in my language and file loaded with below code:
add_action( 'plugins_loaded', 'keydesign_addon_load_textdomain' );
function keydesign_addon_load_textdomain() {
load_plugin_textdomain( 'keydesign', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
but plugin translation not working and it doesn't translate any words.
textdomain is "keydesign" and words are like this
esc_html__("App gallery", "keydesign")
I used "keydesign-addon-fa_IR.mo" and "fa_IR.mo" for file name but not working
also I installed "loco translate" to translate plugin but again not working

Wordpress load back end translate .mo file

How do I load the .mo translate file to the back end of the plugin?
This only for front end:
function my_plugin_load_plugin_textdomain()
{
load_plugin_textdomain( 'my_plgin', FALSE, basename( dirname( __FILE__ ) ) . '/languages/' );
}
add_action( 'plugins_loaded', 'my_plugin_load_plugin_textdomain' );

Problems loading scripts in WordPress

I am working on my first plugin and it is coming along alright. However, I am not able to load my scripts (CSS and JS) from my plugin. This is my code:
function my_scripts() {
wp_enqueue_style('my-style', plugins_url( 'my-plugin/my-style.css') );
wp_enqueue_script( 'my-js', plugins_url( 'my-plugin/my-js.js' ), array('jquery'), '', true );
} add_action( 'wp_enqueue_scripts', 'my_scripts' );
I must be missing something... such a simple code to not be working :(
Your code looks pretty much correct, but check out Plugins URL Page and see this particular part:
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
Basically, you need to load like this:
plugins_url( 'my-plugin/my-js.js' , __FILE__ ),
Well, I got it. My mistake was that I was using the wrong hook! Duh! In the admin side, you use the hook admin_enqueue_scripts and in the front side you use wp_enqueue_scripts to hook your function to enqueue your scripts to WordPress.
Here is a simple example to import from your plugin. If you want to enqueue scripts from your theme, you should use get_template_directory_uri() function to get the proper path.
From your custom plugin to the admin side:
function load_my_scripts() {
wp_enqueue_style('my-style', plugins_url( 'my-style.css', __FILE__ ));
wp_enqueue_script( 'my-js', plugins_url( 'my-js.js', __FILE__ ), array('jquery'), '', true );
} add_action( 'admin_enqueue_scripts', 'guide_express_scripts' );
From your custom plugin to the front side:
function load_my_scripts() {
wp_enqueue_style('my-style', plugins_url( 'my-style.css', __FILE__ ));
wp_enqueue_script( 'my-js', plugins_url( 'my-js.js', __FILE__ ), array('jquery'), '', true );
} add_action( 'wp_enqueue_scripts', 'guide_express_scripts' );

Is there any way to override a WordPress template with a plugin?

I'd like to make a landing page. If plugin detects some GET or POST requests it should override wordpress theme and show its own.
It would work somehow like that:
if (isset($_GET['action']) && $_GET['action'] == 'myPluginAction'){
/* do something to maintain action */
/* forbid template to display and show plugin's landing page*/
}
I'm familiar with WP Codex, but I don't remember if there is any function to do that. Of course, I googled it with no results.
Thanks for any ideas in advance.
You need the hook template_include. It doesn't seem documented in the Codex, but you can find more examples here in SO or in WordPress StackExchange
Plugin file
<?php
/**
* Plugin Name: Landing Page Custom Template
*/
add_filter( 'template_include', 'so_13997743_custom_template' );
function so_13997743_custom_template( $template )
{
if( isset( $_GET['mod']) && 'yes' == $_GET['mod'] )
$template = plugin_dir_path( __FILE__ ) . 'my-custom-page.php';
return $template;
}
Custom Template in Plugin folder
<?php
/**
* Custom Plugin Template
* File: my-custom-page.php
*
*/
echo get_bloginfo('name');
Result
Visiting any url of the site with ?mod=yes will render the plugin template file, e.g.: http://example.com/hello-world/?mod=yes.
you need to create a folder '/woocommerce/' inside your plugin directory, inside woocommerce you need to add a folder say, for email 'emails' and put the required template inside '/emails/' to override. just copy paste this code in the main.php of your plugin.
<?php
/**
* Plugin Name: Custom Plugin
*/
function myplugin_plugin_path() {
// gets the absolute path to this plugin directory
return untrailingslashit( plugin_dir_path( __FILE__ ) );
}
add_filter( 'woocommerce_locate_template', 'myplugin_woocommerce_locate_template', 10, 3 );
function myplugin_woocommerce_locate_template( $template, $template_name, $template_path ) {
global $woocommerce;
$_template = $template;
if ( ! $template_path ) $template_path = $woocommerce->template_url;
$plugin_path = myplugin_plugin_path() . '/woocommerce/';
// Look within passed path within the theme - this is priority
$template = locate_template(
array(
$template_path . $template_name, $template_name
)
);
// Modification: Get the template from this plugin, if it exists
if ( ! $template && file_exists( $plugin_path . $template_name ) )
$template = $plugin_path . $template_name;
// Use default template
if ( ! $template )
$template = $_template;
// Return what we found
return $template;
}
?>
for reference template override using plugin

How to Integrate Option Tree for Wordpress theme

I want to integrate Option Tree framework with Wordpress theme without installing and activating plugin then how to do it ?
Since version 2.0 the plugin developer has included a number of filters that can be used in your functions.php. These include Theme Mode, and the comments within ot-loader.php state;
* For developers: Theme mode.
*
* Run a filter and set to true to enable OptionTree theme mode.
* You must have this files parent directory inside of
* your themes root directory. As well, you must include
* a reference to this file in your themes functions.php.
* #since 2.0
*/
define( 'OT_THEME_MODE', apply_filters( 'ot_theme_mode', false ) );
To activate Options Tree in your theme rather than as a plugin you include all the plugin files in the theme's root directory, ie
/wp-content/themes/my-awesome-theme/options-tree
and in functions.php you'd run this filter and then include the ot-loader.php file. I've shown this below, and I have also shown the show_pages filter;
add_filter( 'ot_theme_mode', '__return_true' );
add_filter( 'ot_show_pages', '__return_true' );
require_once ('option-tree/ot-loader.php');
The show_pages filter is useful because after you have set up your theme and your options you would then go in and set it to false so the client isn't given the main Options Tree admin menu and therefore can't start 'tinkering' and trash everything. You change it to;
add_filter( 'ot_show_pages', '__return_false' );
For anyone using a child theme and getting "failed to open stream" errors when using the OptionTree plugin in Theme Mode, do the following:
ot-loader.php, around line 128, change this:
if ( false == OT_THEME_MODE ) {
define( 'OT_DIR', plugin_dir_path( __FILE__ ) );
define( 'OT_URL', plugin_dir_url( __FILE__ ) );
} else {
define( 'OT_DIR', trailingslashit( get_template_directory() ) . trailingslashit( basename( dirname( __FILE__ ) ) ) );
define( 'OT_URL', trailingslashit( get_template_directory_uri() ) . trailingslashit( basename( dirname( __FILE__ ) ) ) );
}
To this:
if ( false == OT_THEME_MODE ) {
define( 'OT_DIR', plugin_dir_path( __FILE__ ) );
define( 'OT_URL', plugin_dir_url( __FILE__ ) );
} elseif ( is_child_theme() ) {
define( 'OT_DIR', trailingslashit( get_stylesheet_directory() ) . trailingslashit( basename( dirname( __FILE__ ) ) ) );
define( 'OT_URL', trailingslashit( get_stylesheet_directory_uri() ) . trailingslashit( basename( dirname( __FILE__ ) ) ) );
} else {
define( 'OT_DIR', trailingslashit( get_template_directory() ) . trailingslashit( basename( dirname( __FILE__ ) ) ) );
define( 'OT_URL', trailingslashit( get_template_directory_uri() ) . trailingslashit( basename( dirname( __FILE__ ) ) ) );
}
The code checks if the theme in use is a child theme ( is_child_theme() ) and sets the dir and url using get_stylesheet_directory() and get_stylesheet_directory_uri().
Hope this helps out anyone else running into this issue.
It's really easy to integrate option tree :
Visit link below if you like to use it using same plugin slug:
Using same plugin slug
Or you can ingrate it in custom folder on your WordPress theme:
Using custom folder
Video Guide here (3:44 Sec):
Video Guide
add_filter('ot_show_pages','__return_false');
include_once('inc/theme-options.php');
Export setting in theme-options.php file.
add_filter('ot_theme_mode','__return_true');
require( trailingslashit( get_template_directory() ) . 'theme-option/ot-loader.php' );
add_filter('ot_show_new_layout','__return_false');

Resources