why my plugin don't traslate wordpress - php

i'm doing a wordpress plugin.
my_plugin.php
/*
* Plugin Name: my plugin
* Author: Vendetta
* Author URI: http://abel-olguin.com
* Text Domain: my_plugin
* Domain Path: /languages
*/
*.po and *.mo files are in lenguages and i call traslate whit
__("text in po", "my_plugin")
files *.po and *.mo in directory languages names:
my_plugin-en_US.po, my_plugin-es_ES.po...
but strings are not translated, always show everything in English and my browser is in spanish.
what it's wrong?

You need to load your plugin’s translations files using the function load_plugin_textdomain().
function my_plugin_load_plugin_textdomain() {
load_plugin_textdomain( 'my-plugin', FALSE, basename( dirname( __FILE__ ) ) . '/languages/' );
}
add_action( 'plugins_loaded', 'my_plugin_load_plugin_textdomain' );
Read more here: http://ottopress.com/tag/internationalization/

Related

WordPress Load .json Script Translations from Child Theme

I'm NOT a WordPress theme author! I want to load .json translations that comes from related admin .js string files inside a WordPress theme. I succeed to load translations by the following function:.
add_action( 'admin_enqueue_scripts', 'kadence_child_js_translations' );
function kadence_child_js_translations() {
if ( get_locale() == 'fa_IR' ) {
$handle = 'my-kadence-customizer-js';
$js_uri = get_template_directory_uri() . '/inc/dashboard/react/src/customizer.js';
$domain = 'kadence';
$json_path = get_stylesheet_directory() . '/languages';
wp_register_script( $handle, $js_uri );
wp_enqueue_script( $handle );
wp_set_script_translations( $handle, $domain, $json_path );
}
}
When I refresh the admin panel, Translations are loaded But I get this error in console:
Uncaught SyntaxError: import declarations may only appear at top level of a module ( customizer.js:6 )
I tried to use the load_script_textdomain() function instead of wp_set_script_translations() but not only none of translations are loaded but also I have this error again!
How can I load .json scripts of a WordPress Theme from a child theme correctly?

How to create a WordPress plugin that registers a custom sidebar widget

I have the following use case. I have created a custom "search" functionality, all it is an input box that is using some API fetch and jquery magic.
Essentially, I want to add this search function into its own custom sidebar navigation widget via a plugin installation.
I wish to create a plugin that registers a custom "menu" sidebar widget. The sidebar widget will drop this search functionality to were ever the widget is "dragged and dropped". The display is for only the front of the site. You know, how the default search widget works.
Some Facts:
I have created a boiler plated plugin in wp-contents, it had a blank
activate, deactivate and uninstall function within the mail plugin class and I have hooked the activate and deactivate button.
The source code for the "custom search functionality" is within the
same folder as the plugin.
Folder scheme looks like:
MBE PLUGIN
index.php
mbe-plugin.php
api-search.html
assets
css
scripts
ETC
In my plugin.php file, what must I include in the "activation" function to achieve this?
This is my boilerplate plugin code... pretty empty
.
<?php
/**
* #package MBEPlugin
*/
/*
Plugin Name: MBE Plugin
Plugin URI: http://localhost/mbeTest/plugin
Description: This plugin is for the search modal for MBE website. Project 'accordion'
Version: 1.0.0
Author: Erick Guerra
Author URI: http://bashs3c.com
License: GPLv2 or later
Text Domain: mbe-plugin
*/
// Secuirty measure, checks variable in env to ensure we are in WP
if ( ! defined( 'ABSPATH' ) ) {
echo 'These are not the drones you are looking for!';
exit;
}
class MbePlugin {
function activate() {
$searchHTML = file_get_contents(plugins_url('api-search.html', __FILE__));
echo $searchHTML;
// generate a CPT
// Flush the rewrite rules
}
function deactivate() {
// Flush rewrite rules
}
function uninstall() {
// delete CPT
// Delete all the plugin data from DB
}
function displaySearch() {
$searchHTML = file_get_contents(plugins_url('api-search.html', __FILE__));
echo $searchHTML;
}
}
if (class_exists('MbePlugin')) {
$mbePlugin = new MbePlugin();
}
//activation
register_activation_hook( __FILE__, array( $mbePlugin, 'activate' ) );
//deactivation
register_deactivation_hook( __FILE__, array( $mbePlugin, 'deactivate' ) );
//uninstall
//
//add_actions('wp_enqueue_scripts', 'search_init');

how to override parent functions.php in child functions.php

I am looking for some suggestions on how to override or replace some functions that are loading in a parent theme and move them into the child theme so we can make alterations to the files themselves in the child theme.
We have duplicated the structure and files of the parent theme into the child theme, but can't yet find a way to prevent the parent theme from loading and load the child theme files instead.
Essentially it's all the require files listed below that we will duplicate and alter in the child theme, but need to find some way to override the parents functions.php.
We have tried multiple ways to do this but just can't seem to get it to work so far.
This is the current parent functions.php file:
/**
* moto functions and definitions
*
* #package moto
*/
if ( ! function_exists( 'moto_setup' ) ) :
/**
* Sets up theme defaults and registers support for various WordPress features.
*
* Note that this function is hooked into the after_setup_theme hook, which
* runs before the init hook. The init hook is too late for some features, such
* as indicating support for post thumbnails.
*/
function moto_setup() {
global $pagenow;
if ( is_admin() && 'themes.php' == $pagenow && isset( $_GET['activated'] ) ) {
wp_redirect(admin_url("options-general.php?page=moto-system-status")); // Your admin page URL
exit();
}
/* snip irrelevant code */
}
endif; // moto_setup
add_action( 'after_setup_theme', 'moto_setup' );
/* snip irrelevant code */
add_action( 'after_setup_theme', 'moto_content_width', 0 );
/**
* Register widget area.
*
* #link http://codex.wordpress.org/Function_Reference/register_sidebar
*/
function moto_widgets_init() {
/* snip irrelevant code */
}
add_action( 'widgets_init', 'moto_widgets_init' );
/**
* Implement the Custom Header feature.
*/
require get_template_directory() . '/function/custom-header.php';
/**
* Custom template tags for this theme.
*/
require get_template_directory() . '/function/template-tags.php';
/**
* Custom functions that act independently of the theme templates.
*/
require get_template_directory() . '/function/extras.php';
/**
* Customizer additions.
*/
require get_template_directory() . '/function/customizer.php';
/**
* Load Jetpack compatibility file.
*/
require get_template_directory() . '/function/jetpack.php';
require_once get_template_directory() . '/include/aq_resizer.php';
require_once get_template_directory() . '/include/moto-sys-req.php';
require_once get_template_directory() . '/include/moto-enqueue.php';
require_once get_template_directory() . '/include/moto-functions.php';
require_once get_template_directory() . '/include/theme_plugin/plugin-activate-config.php';
require_once get_template_directory() . '/include/wordpress-reset.php';
Any suggestions?
Thank you in advance.

Cron event is not executed in custom Wordpress plugin

I have a site where i need to import data daily from an external url so i made a plugin to handle this. So far so good, but the thing is that my cron event doesn't work. I installed Crontrol plugin to test the event, but nothing happens. I see my hook name in the list, but when i click on 'Run now' i get a message that the cron event is successfully executed, but the data isn't imported.
I've searched through a lot of recourses online (for example), but somehow all the solutions posted elsewhere don't seem to work for me. I must be missing a step somewhere.
The plugin is called import-data and in wp-content/plugins/import-data/ i have import-data.php:
<?php
/**
* Plugin Name: Import data
* Plugin URI:
* Description: Import data
* Version: 1.0.0
* Author:
* Author URI:
* License: GPL2
*/
// Block direct acces to file
defined('ABSPATH') or die();
// Include functions
require_once dirname( __FILE__ ).DIRECTORY_SEPARATOR.'functions.php';
// Include class
require_once dirname( __FILE__ ).DIRECTORY_SEPARATOR.'lib/class.import_data.php';
/**
* #desc iterate through all posts and update information
*/
function import_data(){
$wp_query = new WP_Query(
array(
'post_type' => 'post',
'post_status' => 'publish',
)
);
if($wp_query->have_posts()){
while($wp_query->have_posts()){
$wp_query->the_post();
$post_id = $wp_query->post->ID;
$external_id = get_field(trim(get_option('acfname_external_id')));
// Execute plugin
Import_Data::getInstance()->fetchDetails($external_id, $post_id);
}
wp_reset_postdata();
}
}
/**
* Set cron
*/
function my_event(){
if(!wp_next_scheduled('import_data')){
wp_schedule_event(time(), 'daily', 'import_data');
}
}
add_action('wp', 'my_event');
function unset_event(){
wp_clear_scheduled_hook('import_data');
}
register_deactivation_hook(__FILE__, 'unset_event');
I know that the method fetchDetails() works because i tested the output before and when i manually run it (i've added a shortcode to import_data() and used that on a demo page) the data gets imported, but the cron settings above don't.
In functions.php are only admin page settings.
This are my first steps in the world of plugin development for Wordpress so i can image that i miss an important hook or filter (or whatever), but i just can't find what it is. Perhaps some initialisation?
First of you should prefix your global php functions to avoid conflicts with other plugins, themes or core.
I would use the activation hook to schedule the event, here is how I would do this:
<?php
/**
* Plugin Name: Import data
* Plugin URI:
* Description: Import data
* Version: 1.0.0
* Author:
* Author URI:
* License: GPL2
*/
// Block direct acces to file
defined('ABSPATH') or die();
// Include functions
require_once dirname( __FILE__ ).DIRECTORY_SEPARATOR.'functions.php';
// Include class
require_once dirname( __FILE__ ).DIRECTORY_SEPARATOR.'lib/class.import_data.php';
// Register activation / deactivation hooks
register_deactivation_hook( __FILE__, 'ip_deactivation_func' );
register_activation_hook( __FILE__, 'ip_activation_func' );
// The plugin activation function
function ip_activation_func(){
// Do not forget to namespace your hooks also
if( !wp_next_scheduled( 'ip_import_data' ) ){
wp_schedule_event( time(), 'daily', 'ip_import_data' );
}
}
// The plugin deactivation function
function ip_deactivation_func(){
wp_clear_scheduled_hook( 'ip_import_data' );
}
// Add the action event hook
add_action( 'ip_import_data', 'ip_do_import_data' );
// Your actual event code:
function ip_do_import_data(){
$wp_query = new WP_Query(
array(
'post_type' => 'post',
'post_status' => 'publish'
)
);
if( $wp_query->have_posts() ){
while($wp_query->have_posts()){
$wp_query->the_post();
// Added this part, no need to use: $wp_query object here!
global $post;
$post_id = $post->ID;
$external_id = get_field( trim( get_option( 'acfname_external_id' ) ) );
// Execute plugin
Import_Data::getInstance()->fetchDetails( $external_id, $post_id );
}
wp_reset_postdata();
}
}
I do not know about your event code, you might need to run it to make sure its working correctly.
Learn more about WP cron here:
https://developer.wordpress.org/plugins/cron/
Learn more about activation / deactivation hooks here:
https://developer.wordpress.org/plugins/the-basics/activation-deactivation-hooks/
And a good plugin to debug wp cron events:
https://wordpress.org/plugins/wp-crontrol/

Create release zip file on Github without tag name in it

I have this repo on Github - https://github.com/ronakg/awesome-flickr-gallery-plugin. It's a wordpress plugin for creating a photo gallery from your photos stored on Flickr.
Now what I want to achieve is - when I create a new release zip for my plugin, it should not use the tag name.
For example, I create releases 3.5.0 and 3.6.0. The folder structure for both the releases should be same.
awesome-flickr-gallery-plugin
/index.php
/README.txt
.
.
Right now it creates the release zipfiles like this:
awesome-flickr-gallery-plugin-3.5.0
/index.php
/README.txt
.
.
This is important for me as I want to serve this zip files directly as WordPress plugin updates for my users. This different file structure breaks the plugin update process in WordPress.
Any ideas?
I faced a similar problem with the prefix -master and solved with the following filter upgrader_source_selection. My repository is github-plugin-for-wordpress, adjust for your own.
/**
* Access this plugin’s working instance
*
* #wp-hook plugins_loaded
* #return object of this class
*/
public function plugin_setup()
{
add_filter( 'upgrader_source_selection', array( $this, 'rename_github_zip' ), 1, 3);
}
/**
* Removes the prefix "-master" when updating from GitHub zip files
*
* See: https://github.com/YahnisElsts/plugin-update-checker/issues/1
*
* #param string $source
* #param string $remote_source
* #param object $thiz
* #return string
*/
public function rename_github_zip( $source, $remote_source, $thiz )
{
if( strpos( $source, 'github-plugin-for-wordpress') === false )
return $source;
$path_parts = pathinfo( $source );
$newsource = trailingslashit( $path_parts['dirname'] ) . trailingslashit( 'github-plugin-for-wordpress' );
rename( $source, $newsource );
return $newsource;
}

Categories