Wordpress: Get pll_the_languages inside my plugin - php

How can i get languages into my custom plugin of wordpress?
When i call pll_the_languages() it's output me error.
Maybe i should call some global method?
My plugin code:
/**
* woocommerce-admin-ajax
*
* #package woocommerce-admin-ajax
* #author Sergey Samokhvalov
* #wordpress-plugin
*
* Plugin Name: WooCommerce Admin Ajax
* Plugin URI: https://redirex.studio
* Description: Additional functionality for ajax update of product in admin page without reload page.
* Version: 1.0
* Requires PHP: 5.6.20
* Author: Sergey Samokhvalov
* Author URI: https://github.com/RedirexStudio/woocommerce-admin-ajax
* Text Domain: Woocommerce Admin Ajax
*/
/* Registrate admin js and styles */
add_action( 'admin_enqueue_scripts', 'reg_amin_js' );
function reg_amin_js( $page ) {
// change to the $page where you want to enqueue the script
if( $page == 'post-new.php' || $page == 'post.php' || $page == 'edit.php' ) {
// Enqueue WordPress media scripts
wp_enqueue_media();
// Enqueue custom script that will interact with wp.media
wp_enqueue_script( 'woocommerce_admin-scripts', plugins_url('/admin/js/admin.js',__FILE__ ), array('jquery') );
// Enqueue custom styles for admin panel
wp_enqueue_style('woocommerce_admin-styles', plugins_url('/admin/css/style.css', __FILE__ ));
}
}
/* //END//Registrate admin js and styles */
/* Polylang capability */
pll_the_languages();
/* //END//Polylang capability */

My issue is solved.
You should use admin_init action:
function plugin_pll_register_string() {
if ( function_exists( 'pll_register_string' ) ) {
pll_register_string('woocommerce-admin-ajax-strings', 'Update');
pll_register_string('woocommerce-admin-ajax-strings', 'Published');
pll_register_string('woocommerce-admin-ajax-strings', 'Product is updated!');
}
}
add_action( 'admin_init', 'plugin_pll_register_string' );

Related

How to enqueue scripts in my plugin

I need to use JavaScript (JQuery) within my plugin. The enqueueing should not be inside the themes' function.php. Imagine telling your users "Please to add this line of code in your functions.php etc..."
/plugins/foo-plugin/foo-plugin.php:
<?php
/**
* Plugin Name: Foo Plugin
* Plugin URI: foobar.com
* Description: ___DEV___
* Version: 1.0.0
*/
if ( ! defined( 'ABSPATH' ) ) exit;
function Foo_Plugin() {
return Foo_Plugin::instance();
} // End Foo_Plugin()
add_action( 'plugins_loaded', 'Foo_Plugin' );
/**
* Load Scrips
*/
function enqueue_js() {
// JS
wp_enqueue_script( 'foo', './assets/js/foo.js', false );
}
add_action('wp_enqueue_scripts', 'enqueue_js');
[...]
I've followed this but I'm not getting my output:
//foo.js:
$(document).ready(function(){
console.log("foo is ready")
})
I have deactivated/reactivated the plugin and nothing. How to use JS/CSS in my plugin? As you've guested, it's my first time creating one ;)
Path to JS:
/plugins/foo-plugin/assets/js/foo.js
In the admin section, use the admin_enqueue_scripts action hook.
function enqueue_js() {
wp_enqueue_script( 'foo', './assets/js/foo.js');
}
add_action('admin_enqueue_scripts', 'enqueue_js');
Also, don't put that false in there. The default should be an empty array if there are no dependencies.

Show Stylesheet only on Plugin page WordPress

I have a lot of CSS scripts in my WordPress plugin which may affect the other WordPress tags, such as form as an example:
.form {
padding:30px;
background-color:#fff;
}
I can change the CSS, but I want to have a script which only allows the stylesheet to show on the plugin page.
So to clear it up, currently the stylesheet <link> is always in the source code of the admin panel, but I want a script which only puts the stylesheet <link> in the source code when the user is on the plugin page.
You can use get_current_screen:
$screen = get_current_screen();
if ( $screen->id == 'your_plugin_page' ) ){
$custom_css = ".form {.....}";
wp_add_inline_style( 'your_main_style_handle', $custom_css );
}
Update 1:
To add a submenu to your admin menu:
add_action( 'admin_menu', 'your_plugin_admin_menu' );
//Add this to register you styles
add_action( 'admin_init', 'your_plugin_admin_init' );
then:
/**
* Register your stylesheet.
*/
function wpdocs_plugin_admin_init() {
wp_register_style('your-style', plugins_url('scripts/jquery-ui.css',__FILE__ ));
wp_register_script( 'jquery-ui', plugins_url('scripts/jquery-ui.js',__FILE__ ));
}
/**
* Register your plugin page and hook stylesheet loading.
*/
function your_plugin_admin_menu() {
$page = add_submenu_page(...., 'your_plugin_manage_menu' );
//Call 'your_plugin_admin_styles' only on the plugin’s options page
add_action( "admin_print_styles-{$page}", 'your_plugin_admin_styles');
}
/**
* Enqueue our stylesheet.
*/
function your_plugin_admin_styles() {
wp_enqueue_style('your-style');
wp_enqueue_script('jquery-ui');
$custom_css = ".form {.....}";
wp_add_inline_style( 'your-style', $custom_css );
}
/**
* Output our admin page.
*/
function your_plugin_manage_menu() {
// ...
}
you can create style-sheet, and create hook for that style-sheet reference. Then you can add WordPress using add_action(), within plugin call, where you like to call that script. So it will be referenced at head of page for on specified page as needed.
Read more on
https://developer.wordpress.org/reference/functions/wp_enqueue_script/
https://developer.wordpress.org/reference/functions/add_action/

Developing Wordpress-Plugin: Problems with loading .js into head and HTML-Snippet as a Shortcode

I was asked to develope a Wordpress plugin for a project I am currently involved in, since I normally do Graphics and UX Design (CSS3). I am not that familiar with developing plugins for WP, although I've got quite some understanding of PHP. I've got the following code:
<?php
/**
* Plugin Name: ExpButton Wordpress Plugin
* Plugin URI: https://url.de
* Description: Ein Generator des Expbuttons
* Version: 0.1
* Author: Wilko Meyer
* Author URI: http://url.com
**/
/**
* Loading js into header
**/
function add_async($url)
{
if (strpos($url, '#asyncload')===false)
return $url;
else if (is_admin())
return str_replace('#asyncload', '', $url);
else
return str_replace('#asyncload', '', $url)."' async='async";
}
add_filter('clean_url', 'add_async', 11, 1);
function expertbuttonjs()
{
// Loading the JS
wp_register_script( 'custom-script', plugins_url( 'https://www.expert-button.de/js.js#asyncload', __FILE__ ), array( 'jquery', 'jquery-ui-core' ), '20120208', true );
wp_enqueue_script( 'custom-script' );
}
add_action( 'wp_enqueue_scripts', 'wptuts_scripts_with_the_lot' );
/**
*Creating Shortcode
**/
add_shortcode( 'shortcode', 'expbutton' );
function expbutton( $atts ) {
/* Turn on buffering */
ob_start(); ?>
<div style="overflow:hidden;font-size:9px;height:auto;width:auto;text-align:center;margin:auto;" id="expertbuttonbg"><a target="_bl$
<?php
/* Get the buffered content into a var */
$sc = ob_get_contents();
/**
*Expbutton to Shortcode
**/
add_shortcode( 'shortcode', 'expertbutton' );
function expertbutton( $atts ) {
ob_start(); ?>
<div style="overflow:hidden;font-size:9px;height:auto;width:auto;text-align:center;margin:auto;" id="expertbuttonbg"><a target="_bl$
<?php
$sc = ob_get_contents();
ob_end_clean();
/* Return the content as usual */
return $sc;
}
?>
The purpose of this plugin is to load the js.js as async into the of the page and to create an shortcode from the HTML Code below the /* Turn on buffering */ section which can be used at wordpress sites.
Currently the code does not load the js into header and the shortcode which should be created does not work either and I have no Idea why. Hope someone can help me with this problem & has some clue.
Why the plugin doesn't work?
Thanks in advance.
I have made some important littles changes. The html code of your buttons is incomplete (so I have try to guess). The name ou your javascript file seems to me very strange and unusual...
<?php
/**
* Plugin Name: ExpButton Wordpress Plugin
* Plugin URI: https://url.de
* Description: Ein Generator des Expbuttons
* Version: 0.1
* Author: Wilko Meyer
* Author URI: http://url.com
**/
/**
* Loading js into header
**/
function add_async($url)
{
if ( strpos($url, '#asyncload') === false )
return $url;
else if ( is_admin() )
return str_replace('#asyncload', '', $url);
else
return str_replace('#asyncload', '', $url)."' async='async";
}
add_filter('clean_url', 'add_async', 11, 1);
There was a problem in the url in wp_register_script() function that I have corrected and in the add_action() too: your javascript file has to be inside your plugin root folder (I mean not in a subfolder). Also your JS file name seems to me very strange (normally it's file finishing with .js extension and not with .js#asyncload):
function expertbuttonjs()
{
// Loading the JS
wp_register_script( 'custom-script', plugins_url('/js.js#asyncload', __FILE__ ), array( 'jquery', 'jquery-ui-core' ), '20120208', true );
wp_enqueue_script( 'custom-script' );
}
add_action( 'wp_enqueue_scripts', 'expertbuttonjs' );
Dont use shortcode name for shortcodes functions. The code of your buttons are incomplete in your code. Also you can't have 2 times the same name for shortcodes or functions:
/*
* expbutton Shortcodes
*/
add_shortcode( 'expbutton', 'expbutton' );
function expbutton( $atts ) {
/* Turn on buffering */
ob_start(); ?>
<div style="overflow:hidden;font-size:9px;height:auto;width:auto;text-align:center;margin:auto;" id="expertbuttonbg"><a target="_blank" href="'.$sc.'">Text Button</a><div>
<?php
/* Get the buffered content into a var */
$sc = ob_get_contents();
/*
* expertbutton Shortcode
*/
add_shortcode( 'expertbutton', 'expertbutton' );
function expertbutton( $atts ) {
ob_start(); ?>
<div style="overflow:hidden;font-size:9px;height:auto;width:auto;text-align:center;margin:auto;" id="expertbuttonbg"><a target="_blank" href="'.$sc.'">Text Button</a><div>
<?php
$sc = ob_get_contents();
ob_end_clean();
/* Return the content as usual */
return $sc;
}
?>
This should work now.

Styling Wordpress Plugin Settings Page

I have created a plugin for wordpress and I am now working on the settings page in the admin area, how do I include a style sheet which is located
plugins/pluginname/assets/css/bootstrap.min.css
Found the solution here:
https://codex.wordpress.org/Function_Reference/wp_enqueue_style
Load stylesheet only on a plugin's options page
/*
* This example will work at least on WordPress 2.6.3,
* but maybe on older versions too.
*/
add_action( 'admin_init', 'my_plugin_admin_init' );
add_action( 'admin_menu', 'my_plugin_admin_menu' );
function my_plugin_admin_init() {
/* Register our stylesheet. */
wp_register_style( 'myPluginStylesheet', plugins_url('stylesheet.css', __FILE__) );
}
function my_plugin_admin_menu() {
/* Register our plugin page */
$page = add_submenu_page( 'edit.php',
__( 'My Plugin', 'myPlugin' ),
__( 'My Plugin', 'myPlugin' ),
'administrator',
__FILE__,
'my_plugin_manage_menu' );
/* Using registered $page handle to hook stylesheet loading */
add_action( 'admin_print_styles-' . $page, 'my_plugin_admin_styles' );
}
function my_plugin_admin_styles() {
/*
* It will be called only on your plugin admin page, enqueue our stylesheet here
*/
wp_enqueue_style( 'myPluginStylesheet' );
}
function my_plugin_manage_menu() {
/* Output our admin page */
}

WordPress Custom Theme > Execute "Setup" Code on Activation only?

I have some setup code in my functions.php file that sets permalinks and adds categories that are used by the theme. I only want this code to run when the theme is first activated. There is no need for the code to run again. However, by placing it in the functions.php file, it runs each and every time a page on the website is loaded.
Is there an alternative method to use so that this code only runs when the custom theme is first activated?
In wp-includes/theme.php you’ll find the function switch_theme(). It offers an action hook:
/**
* Switches current theme to new template and stylesheet names.
*
* #since unknown
* #uses do_action() Calls 'switch_theme' action on updated theme display name.
*
* #param string $template Template name
* #param string $stylesheet Stylesheet name.
*/
function switch_theme($template, $stylesheet) {
update_option('template', $template);
update_option('stylesheet', $stylesheet);
delete_option('current_theme');
$theme = get_current_theme();
do_action('switch_theme', $theme);
}
So you may use this in your functions.php:
function my_activation_settings($theme)
{
if ( 'Your Theme Name' == $theme )
{
// do something
}
return;
}
add_action('switch_theme', 'my_activation_settings');
Just an idea; I haven’t tested it.
Another one to look at would be after_switch_theme it is also in wp-includes/theme.php
It seems to be a hook run in check_theme_switched
/**
* Checks if a theme has been changed and runs 'after_switch_theme' hook on the next WP load
*
* #since 3.3.0
*/
function check_theme_switched() {
if ( $stylesheet = get_option( 'theme_switched' ) ) {
$old_theme = wp_get_theme( $stylesheet );
// Prevent retrieve_widgets() from running since Customizer already called it up front
if ( get_option( 'theme_switched_via_customizer' ) ) {
remove_action( 'after_switch_theme', '_wp_sidebars_changed' );
update_option( 'theme_switched_via_customizer', false );
}
if ( $old_theme->exists() ) {
/**
* Fires on the first WP load after a theme switch if the old theme still exists.
*
* This action fires multiple times and the parameters differs
* according to the context, if the old theme exists or not.
* If the old theme is missing, the parameter will be the slug
* of the old theme.
*
* #since 3.3.0
*
* #param string $old_name Old theme name.
* #param WP_Theme $old_theme WP_Theme instance of the old theme.
*/
do_action( 'after_switch_theme', $old_theme->get( 'Name' ), $old_theme );
} else {
/** This action is documented in wp-includes/theme.php */
do_action( 'after_switch_theme', $stylesheet );
}
flush_rewrite_rules();
update_option( 'theme_switched', false );
}
}`
add_action('after_switch_theme', 'my_activation_settings');
I think both ways work just something else to look into :)

Categories