Wordpress plugin localization- Can't display the translations - php

I am trying to add translation to my WordPress plugin using .po files, but I can't display the translation.
My .po and .mo files are named as follows:
plugin-name-fr_FR.mo
plugin-name-fr_FR.po
and are located in the ./languages/ folder.
In my code, I load the translations like this:
add_action( 'init', 'load_translation');
function load_translation() {
load_plugin_textdomain( 'plugin-name', false, dirname(plugin_basename(__FILE__ )) . '/languages/' );
}
I have also tried using:
add_action( 'plugins_loaded', 'load_translation' );
but it didn't work either.
The header of my plugin contains :
* Text Domain: plugin-name
* Domain Path: /languages
When I check the response value of the load_plugin_textdomain() function, it returns true so the files are being found, but the translations are not displayed.
For displaying my content, I use :
<?= __( 'My text' ); ?>
I don't know what to do, can someone help me please ?

You have to assign the custom textdomain, provided that your pot files are correct as well.
<?= __( 'My text' , 'plugin-name'); ?>

Related

My wordpress plugin isn't installing from a zip file

I created a WP plugin, with minimal PHP code - it's almost JS. When I upload files manually to the WP plugins directory, it works.
But when I'm trying to zip the plugin and install it from a zip file, it doesn't work.
The PHP code itself is minimal.
I created a header with minimal requirements and some code to return html and to registre JS and CSS files.
There are other files too: image files, JS files (2 of them)
When I'm trying to install them, I get a message: The link you followed has expired.
Please try again.
I did everything according to tutorials, checked with several of them, and I have no Idea what I'm doing wrong.
Here's my code:
<?php
/**
* Plugin Name: Plugin name
* Description: Plugin description
* Version: 1.0.1
* Author: Author
*/
function dare2care( $atts, $content, $tag ){
wp_register_style( 'dare2care', 'https://cdn.jsdelivr.net/npm/bulma#0.9.4/css/bulma.min.css' );
wp_enqueue_style('dare2care');
wp_enqueue_script('dare2care-quiz', plugins_url( 'dare2care-quiz.js', __FILE__ ), in_footer:true);
$image_base_url = plugins_url( 'images/', __FILE__ );
$content = '
<div id="quiz" class="container">
... truncated...
</div>
';
return $content;
};
add_shortcode('d2c', 'dare2care');
function register_shortcodes(){
add_shortcode('d2c', 'dare2care');
};
add_action( 'init', 'register_shortcodes');
?>
You have to add this to your theme functions.php
#ini_set( 'upload_max_size' , '120M' );
#ini_set( 'post_max_size', '120M');
#ini_set( 'max_execution_time', '300' );
It's a known issue that needs a deeper approach to avoid. I don't know if I can analyze the whole situation assuming to stackoverflow rules.

WordPress theme - Translations from .PO & .MO files are not working

I've been trying to translate a WordPress theme for days with no success, no matter what I try, the _e translate function doesn't work and the strings just stay the same as they are in the .POT file.
What I've done so far:
Loaded the text domain from the languages folder from inside the theme inside functions.php
function theme_setup() {
load_theme_textdomain( 'domain', get_template_directory() . '/languages' );
}
add_action( 'after_setup_theme', 'theme_setup' );
Wrapped the strings I want to translate in the _e() function, including the text domain.
<?php _e( 'Află mai multe', "domain" ); ?>
The languages folder includes a .POT file generated with EazyPo that includes all strings that are inside _e() functions together with an English translation of my strings with .PO and .MO files called en.PO and en.MO respectively.
Switched the language of the website in WordPress General Settings to English.
What I see from your code is that you action is calling the wrong function.
You should change this: add_action( 'after_setup_theme', 'optimrent_setup' );
To this: add_action( 'after_setup_theme', 'theme_setup' );
Because the second parameter to add_action is to what function the callback should be. So in that code you have above, should display some sort of error if you don't have the function optimrent_setup

Custom Admin page using add_menu_page

I have developed a plugin to write my own customized functions and its already activated. I want to add 1 page on the admin dashboard and using below code but it doesn't seem to be working
add_action('admin_menu', 'addAdminPage');
function addAdminPage() {
add_menu_page('Status Page', 'Status Menu', 'manage_options', '__FILE__', 'AdminPage', 'dashicons-wordpress',90);
}
function AdminPage() {
echo 'Hello';
}
You say it's not working but you haven't told us what the error is. Regardless, if you use this, it should work:
add_action( 'admin_menu', 'addAdminPage' );
function addAdminPage(){
add_menu_page(
__( 'Status Page', 'yourplugintextdomain' ),
'Status Menu',
'manage_options',
'statuspage',
'AdminPage',
'dashicons-wordpress',
90
);
}
function AdminPage(){
esc_html_e( 'Hello', 'yourplugintextdomain' );
}
You shouldn't be attempting to use the ( __FILE__ ) as the slug, so I just made it statuspage. Whenever you're writing custom functionality like this, try to prefix everything with something that relates it back to your plugin. Like: lrnr_statuspage and name your functions stuff like lrnr_AdminPage that way you reduce the risk of running into conflicts. Include your plugin or theme's text domain as well to make what you're building translation ready. It's just good practice.
Update/Correction:
I had the function name incorrect. It will work now.
Here is it running on a site I'm currently developing, I just added the above block of code to the bottom of my functions.php and added your username to the 'Hello' message.

WordPress add_menu_page not showing in backend

I am trying to create a custom plugin using the code below but the tab/link to the page is not appearing in the WordPress Dashboard and I'm not sure what I'm doing wrong. My company-admin.php file is located in my plugins folder inside a folder called company-admin. The file permissions for the php file are 644 incase that is relevant. Can you help?
add_action( 'admin_menu', 'my_admin_menu' );
function my_admin_menu() {
add_menu_page(
'Company Admin',
'Company Admin',
'manage_options',
'company-admin/company-admin.php',
'',
'',
6
);
}
I'm unsure if this is the correct way to navigate to my plugin but I used this link, correct me if its the wrong link;
https://mywebpage.uk/wp-admin/admin.php?page=company-admin%2Fcompany-admin.php
And what I get is the following error;
Sorry, you are not allowed to access this page.
Query Monitor
The message above was triggered by Core.
Call stack:
wp_die()
wp-admin/includes/menu.php:348
the permission file in wordpress must be 755 for work correctly.
For develop a plugin follow the wordpress official guide https://developer.wordpress.org/plugins/intro/
Let me know.
Try to add more code to your path. Instead of 'company-admin/company-admin.php', paste 'your_plagin_folder/company-admin/company-admin.php',
Edited
But more cleanest is using that way:
function wpdocs_register_my_custom_menu_page(){
add_menu_page(
'Custom Menu Title',
'custom menu',
'manage_options',
'custompage',
'my_custom_menu_page',
'',
6
);
}
add_action( 'admin_menu', 'wpdocs_register_my_custom_menu_page' );
/**
* Display a custom menu page
*/
function my_custom_menu_page(){
//print all what you need or include any pages
include_once plugins_url('company-admin/company-admin.php', __FILE__);
}

Why remove_meta_box() function is not working in my plugin?

Im trying to delete the section WordPress News in my dashboard by creating a simple plugin. First of all, i've made a php file in the following directory:
wp-content/plugins/basic-plugin.php
In the basic-plugin.php file i have the following block of code:
<?php
/*
Plugin Name: Basic Plugin
Plugin URI: http://wordpress.org/plugins/basic-plugin/
Description: My first plugin for creating and displaying job opportunities
Author: Rumen Panchev
Version: 1.0
License: GPLv2
*/
function ru_remove_dashboard_widget() {
remove_meta_box( 'dashboard_primary', 'dashboard', 'post_container_1' );
}
add_action( 'wp_dashboard_setup', 'ru_remove_dashboard_widget' );
The problem is that the section is still in the dashboard. Im new in WordPress Development and not sure why is this happening ?
Your approach is correct however you're using the wrong context argument with remove_meta_box(). Dashboard widgets aren't registered under post_container_1.
Valid options for the context argument include 'normal', 'advanced' or 'side'. For the particular dashboard widget you're attempting to remove, you need 'side'.
remove_meta_box( 'dashboard_primary', 'dashboard', 'side' );
Documentation: https://codex.wordpress.org/Function_Reference/remove_meta_box

Categories