WordPress localization is not working on plugin - php

I have a plugin to be translated. I have done following tasks:
Loaded the TextDomain:
$my_td = 'mysignup';
function my_signup_textdomain_init() {
global $my_td;
load_plugin_textdomain( $my_td, false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
add_action('plugins_loaded', 'my_signup_textdomain_init');
Added language files(po, pot, mo) in wp_plugin_folder\my_signup\languages (for Bengali bn_BD):
mysignup.pot
mysignup.mo
bn_BD.po
bn_BD.mo
Changed the language code in wp_config.php file:
define('WPLANG', 'bn_BD');
But problem is nothing is changed. I am not sure what i have done as mistake. I already read lots of articles and answers. Still i need help to know how can i test everything is fine that i have done and how can i solve the issue.
Please help me. Thanks in advance.

First of all - what is global $nl_td; did you mean global $my_td; ?
Second - you seem to miss the .po file ( e.g. mysignup.po ) - and you do not need bn_BD.po and bn_BD.mo
Third - try to load like this ( without the unnecessary variable ) :
load_plugin_textdomain( 'mysignup', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
Fourth - Make sure ALL your strings are like so : __('string','mysignup');
EDIT I after comment :
In your comment you wrote :
__('First Name field is required', $nl_td) and _e('First Name field is required', $nl_td)
Again - As far as I can tell from your very restricted code - $nl_td is not defined . $my_td is defined .
Just replace all those variables with a simple string mysignup .
Your code is probably some kind of cut&paste - but you must understand what those variables mean if you want them to work correctly, or make sure they are consistent all over .
Edit II( now that you edited comment from $nl_td to $my_td it seems you start to understand - edit also your code .. :-) then it will work as long as it is global. but better just write a string . )

Related

Can't Get Wordpress Settings Link To Work

I'm trying to build my first Wordpress plugin and it's giving me a ~LOT~ of grief.
I've got a plugin template I'm using, and I'm following THIS tutorial to turn it into something I can use.
The tutorial says to put a function in [insert plugin name]-admin.php to make 'settings' appear near the plugin on the plugins.php page.
This is what it's supposed to look like:
(source: scotch.io)
The thing is, I have tried inserting the function and I just can't get this 'settings' link to appear.
The code the tutorial says to use is this:
public function add_action_links( $links ) {
$settings_link = array( '' . __('Settings', $this->plugin_name) . '', );
return array_merge( $settings_link, $links );
}
I've tried using this--and similar snippets that I've found on other sites after googling--and none of them work. I know that page= is supposed to link to the page URL, I'm not entirely sure what mine is (is it the plugin slug?). Anyway, I'm currently using the plugin slug after 'page='
If anybody here could help me out with this, it would be greatly appreciated. I know the tutorial I'm using was sloppily written because I have managed to find mistakes in it that were causing errors, and I'm relatively new to PHP and totally new to Wordpress plugins.
Did you also add the beloning add_filter?
Like here:
add_filter( 'plugin_action_links_' . plugin_basename(__FILE__), 'my_plugin_action_links' );
function my_plugin_action_links( $links ) {
$links[] = 'Settings';
$links[] = 'More plugins by WP-Buddy';
return $links;
}
Also see:
https://codex.wordpress.org/Plugin_API/Filter_Reference/plugin_action_links_(plugin_file_name)

how to get a file path of wp-config.php of wordpress site?

I am developing a plugin in which i need to include config and query the wordpress database on click ..My question is
How to get url of wp-config.php dynamically ..I have used
include_once $_SERVER['DOCUMENT_ROOT'] . '/wp-config.php';
but i am getting error
include_once(): Failed opening /wp-config.php' for inclusion (include_path='.:/usr/share/php:/usr/share/pear')
and i have tried many options like
require_once('../../../wp-config.php');
require_once ("../wp-config.php");
I dunno wat is exact path ..
Any help is appreciated ..
In short, you shouldn't be trying to include wp-config.php from your plugin. There are few if any good reasons you would ever take this approach. Purists would go as far as to say it should never be done.
A WordPress plugin already has the proper context to access the global $wpdb variable for querying. Take a look at this example:
global $wpdb;
$results = $wpdb->get_results( $wpdb->prepare("SELECT * FROM {$wpdb->prefix}your_table WHERE column = %s", $your_value) );
Suggested reading:
http://codex.wordpress.org/Class_Reference/wpdb
Querying the database in WordPress should be done through the global $wpdb variable. See the codex entry for that: http://codex.wordpress.org/Class_Reference/wpdb.
If you still want to avoid all the warnings to make good practices of coding, you can try this that include many paths where can find wp-config.php
if( !(include $_SERVER['DOCUMENT_ROOT'].'/wp-config.php') )
if( !(include 'wp-config.php') )
if( !(include '../../../wp-config.php') )
if( !(include '../../../../wp-config.php') )
if( !(include '../../../../../wp-config.php') )
die('<H1>Can\'t include config.</H1>');
Notes:
you can add lines with if( !(include 'my_custom_path/wp-config.php') )
you can't use wp_die() until wp-config be included, then use php function die()

How to use wordpress wpdb class to insert data

I am new to wordpress I am try to create table and insert data to that table. so within the wordpress database I created table called 'album' then I created directory called 'my-codes'(within root directory/ same level with 'wp-admin' , 'wp-content, 'wp-includes' directories)
within that directory I created insert.php then i added following code
<?php
global $wpdb;
$wpdb->insert($wpdb->album , array("ID"=>1, "Name"=>'something'), array("%d", "%s"));
?>
but it give this error Fatal error: Call to a member function insert() on a non-object in C:\wamp\www\wordpress\my-codes\insert.php what is the mistake I did, please help me.
You have to load the Wordpress files within your script, so that you have access to Wordpress' $wpdb object:
require_once( '../wp-load.php' );
This will load all of Wordpress, even the functionality you don't need. If you want to only load the database part, read this article.
Update - The first argument to the insert method should be the name of your table:
$wpdb->insert(
'album',
array("ID"=>1, "Name"=>'something'),
array("%d", "%s")
);
require (dirname(dirname(__FILE__)) . '/wp-blog-header.php');
require (ABSPATH . WPINC . '/compat.php');
require (ABSPATH . WPINC . '/functions.php');
require (ABSPATH . WPINC . '/classes.php');
require_wp_db();
global $wpdb; // Look look! Over here! line 270 in wp-settings.php
if ( !empty($wpdb->error) )
dead_db();
//Execute your query here
I would recommend using the WordPress Options API instead of creating a custom table, unless options won't fit your needs. Also, place all your code in your theme's functions.php. That way, you won't need to load any other WordPress files externally. They're already loaded for you automatically if your code is in functions.php.
Here's a quick example of how to store data in the database using options ( taken from the WP Codex article linked above ):
// Create an option to the database
add_option( $option, $value = , $deprecated = , $autoload = 'yes' );
// Removes option by name.
delete_option( $option );
// Fetch a saved option
get_option( $option, $default = false );
// Update the value of an option that was already added.
update_option( $option, $newvalue );
Options are much easier to implement and generally less error prone than direct database access. :)

Optimize Internationalization script, do i made the good choice?

I'm going ahead to develop the localization files for my project and i am not sure if i am doing the good choice.
<?php
$translation= array(
"sentence" => array ("fr" => "phrase", "it" => "frase")
);
function _($toTranslate = '', $lang = 'en'){
if($toTranslate != ''){
if(!array_key_exists($toTranslate[$lang], $translation))
return $toTranslate;
else
return ${$lang}[$toTranslate];
}
}
?>
I clearly no idea to know if i am doing it well.
Not good enough.
Improvement to your implementation.
Place each language in an single file would be better for performance and maintenance. For example, make a folder called "language", it contains "fr.inc.php", "de.inc.php" and so on. In the config file, you have a line like
$config['language'] = 'de';
You can also load the language code from GET parameter:
$config['language'] = htmlspecialchars($_GET['lang']);
Remember to have check if the language file exists or not (file_exists())
In your bootstrap code, you load the language file (check it before)
require_once LANGUANGE_DIR . '/' . $config['language'] . '.inc.php';
and when you need the language resource string, you call your "_" function, now it should look like
function _($LANG_RES_ID) {
global $lang;
return isset($lang[$LANG_RES_ID] ? $lang[$LANG_RES_ID] : $LANG_RES_ID;
}
2 Use gettext
see php.net: http://www.php.net/gettext
Also, many open source apps are good cases for study, phpMyAdmin, for example.

How to obtain wordpress root url (ABSPATH changes on each page)

I have wordpress installed at:
http://example.com/wordpress/
When on homepage, the ABSPATH constant outputs exactly that, but when you navigate off to some other page, like:
http://example.com/wordpress/contact
the ABSPATH also turns to:
http://example.com/wordpress/contact
The question is, how can I obtain the actual root (marked in bold) no matter on which page I am - without hard-coding it?
I'm a bit confused with why ABSPATH changes value, aren't constants unchangeable once they are defined?
Thanks!
you can use Site_url();... :)
I had the same issue on an admin page. Not only do you have to avoid the extra folder that gets inserted, WP may be installed in a folder itself.
Here is a way, albeit somewhat convoluted and written for clarity, that makes the adjustments for these various items. It does avoid DIRECTORY_SEPARATOR issues as well:
if (!defined(PLUGINUPDATEMGR_DOMAIN))
define("PLUGINUPDATEMGR_DOMAIN", strtolower( $_SERVER['HTTP_HOST'] ) );
$wprootbase = strtolower( site_url() );
$wprootstart = strpos( $wprootbase, PLUGINUPDATEMGR_DOMAIN ) +
strlen( PLUGINUPDATEMGR_DOMAIN ); // + 1 to strip the leading slash/backslash
$wprootend = strlen( $wprootbase );
$wproot = substr( $wprootbase, $wprootstart, $wprootend );
echo "Local WP path = '" . $wproot . '"';
Shaken, not stirred, output:
Local WP path = '/wp/wordpress-3.4.2"
Of course, YMMV =;?)
ABSPATH returns the absolute path to the php file in the server such as /var/www/wordpress/, do check your wordpress install.
The answer to your problem is to use:
site_url()
or
bloginfo()
As the people above mentioned.
You could also use
$var = get_bloginfo('wpurl');
To get the value into a variable
The correct answer is
<?php echo esc_url( home_url( '/' ) ); ?>
Here is some text because I must have at least 30 characters

Categories