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' );
Following php file gets a custom demo sidebar to show up in the admin widget menu, but not on actual posts (file located in folder with same name, which is located in the plugin folder in WP file directory) – add a text widget to custom sidebar to test:
<?php
/**
* Plugin Name: Single Post CTA
* Plugin URI: https://github.com/cdils/single-post-cta
* Description: Adds sidebar (widget area) to single posts
* Version: 0.1
* Author: Carrie Dils
* Author URI: https://carriedils.com
* License: GPL v2+
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: spc
*/
// If this file is called directly, abort
if ( !defined( 'ABSPATH' ) ) {
die;
}
/**
* Load stylesheet
*/
function spc_load_stylesheet() {
if ( is_single() ) {
wp_enqueue_style( 'spc_stylesheet', plugin_dir_url(__FILE__) .'spc-styles.css' );
}
}
// Hook stylesheet
add_action( 'wp_enqueue_scripts', 'spc_load_stylesheet' );
// Register a custom sidebar
function spc_register_sidebar() {
register_sidebar( array(
'name' => __( 'Single Post CTA', 'spc' ),
'id' => 'spcsidebar',
'description' => __( 'Displays widget area on single posts', 'spc' ),
'before_widget' => '<div class="widget spc">',
'after_widget' => '</div>',
'before_title' => '<h2 class="widgettitle spc-title">',
'after_title' => '</h2>',
) );
}
// Hook sidebar
add_action( 'widgets_init', 'spc_register_sidebar' );
// Display sidebar on single posts
function spc_display_sidebar( $content ) {
if ( is_single() ) {
dynamic_sidebar( 'spcsidebar' );
}
return $content;
}
// Add dynamic sidebar
add_filter( 'the content', 'spc_display_sidebar' );
Here is the associated style sheet located in the same folder as the file for the custom sidebar:
.spc {
background: gray;
color: blue;
}
The widgets menu under customizer says “Your theme has 1 other widget area, but this particular page doesn’t display it”. This WordPress guide https://developer.wordpress.org/themes/functionality/sidebars/ appears to indicate that one has to register the sidebar/widget in the theme or child theme’s functions.php file and then create a sidebar-{name}.php file in which to run the dynamic_sidebar function. Is this the way instead? I’m using the Genesis Sample child theme, and switching to 2020 and 2017 wordpress themes, or deactivating all other plugins has not fixed problem.
The filter should be add_filter( 'the_content', 'spc_display_sidebar' );. You forgot the underscore.
If you are trying to display the sidebar in a page post type thenis_single() is not going to work. Try is_singular()instead.
Mistack is in hook not "the content", use "the_content"
Ex.
add_filter( 'the_content', 'spc_display_sidebar' );
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/
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
ive been working on my wordpress website source code, when I try to reload the page I get this error:
Parse error: syntax error, unexpected end of file in /home3/snesni/public_html/wp-content/themes/Activello-master/functions.php
on line 284
My function.php line 284-285 says:
endif; // activello_woo_setup add_action( 'after_setup_theme', 'activello_woo_setup' );
And I cant seem to change WP theme. Not sure what to do.
Below is the function.php.
<?php
/**
* activello functions and definitions
*
* #package activello
*/
/**
* Set the content width based on the theme's design and stylesheet.
*/
if ( ! isset( $content_width ) ) {
$content_width = 1090; /* pixels */
}
/**
* Set the content width for full width pages with no sidebar.
*/
function activello_content_width() {
if ( is_page_template( 'page-fullwidth.php' ) ) {
global $content_width;
$content_width = 1008; /* pixels */
}
}
add_action( 'template_redirect', 'activello_content_width' );
if ( ! function_exists( 'activello_main_content_bootstrap_classes' ) ) :
/**
* Add Bootstrap classes to the main-content-area wrapper.
*/
function activello_main_content_bootstrap_classes() {
if ( is_page_template( 'page-fullwidth.php' ) ) {
return 'col-sm-12 col-md-12';
}
return 'col-sm-12 col-md-8';
}
endif; // activello_main_content_bootstrap_classes
if ( ! function_exists( 'activello_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 activello_setup() {
/*
* Make theme available for translation.
* Translations can be filed in the /languages/ directory.
*/
load_theme_textdomain( 'activello', get_template_directory() . '/languages' );
// Add default posts and comments RSS feed links to head.
add_theme_support( 'automatic-feed-links' );
/**
* Enable support for Post Thumbnails on posts and pages.
*
* #link http://codex.wordpress.org/Function_Reference/add_theme_support#Post_Thumbnails
*/
add_theme_support( 'post-thumbnails' );
add_image_size( 'activello-featured', 1170, 550, true );
add_image_size( 'activello-slider', 1920, 550, true );
add_image_size( 'activello-thumbnail', 330, 220, true );
add_image_size( 'activello-medium', 640, 480, true );
// This theme uses wp_nav_menu() in one location.
register_nav_menus( array(
'primary' => esc_html__( 'Primary Menu', 'activello' )
) );
// Enable support for Post Formats.
add_theme_support( 'post-formats', array(
'video',
'audio',
) );
// Setup the WordPress core custom background feature.
add_theme_support( 'custom-background', apply_filters( 'activello_custom_background_args', array(
'default-color' => 'FFFFFF',
'default-image' => '',
) ) );
// Enable support for HTML5 markup.
add_theme_support( 'html5', array(
'comment-list',
'search-form',
'comment-form',
'gallery',
'caption',
) );
/*
* Let WordPress manage the document title.
* By adding theme support, we declare that this theme does not use a
* hard-coded <title> tag in the document head, and expect WordPress to
* provide it for us.
*/
add_theme_support( 'title-tag' );
}
endif; // activello_setup
add_action( 'after_setup_theme', 'activello_setup' );
/**
* Register widgetized area and update sidebar with default widgets.
*/
function activello_widgets_init() {
register_sidebar( array(
'name' => esc_html__( 'Sidebar', 'activello' ),
'id' => 'sidebar-1',
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => '</aside>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
));
register_widget( 'activello_social_widget' );
register_widget( 'activello_recent_posts' );
register_widget( 'activello_categories' );
register_widget( 'activello_instagram_widget' );
}
add_action( 'widgets_init', 'activello_widgets_init' );
/* --------------------------------------------------------------
Theme Widgets
-------------------------------------------------------------- */
require_once(get_template_directory() . '/inc/widgets/widget-categories.php');
require_once(get_template_directory() . '/inc/widgets/widget-social.php');
require_once(get_template_directory() . '/inc/widgets/widget-recent-posts.php');
require_once(get_template_directory() . '/inc/widgets/widget-instagram.php');
/**
* This function removes inline styles set by WordPress gallery.
*/
function activello_remove_gallery_css( $css ) {
return preg_replace( "#<style type='text/css'>(.*?)</style>#s", '', $css );
}
add_filter( 'gallery_style', 'activello_remove_gallery_css' );
/**
* Enqueue scripts and styles.
*/
function activello_scripts() {
// Add Bootstrap default CSS
wp_enqueue_style( 'activello-bootstrap', get_template_directory_uri() . '/inc/css/bootstrap.min.css' );
// Add Font Awesome stylesheet
wp_enqueue_style( 'activello-icons', get_template_directory_uri().'/inc/css/font-awesome.min.css' );
// Add Google Fonts
wp_enqueue_style( 'activello-fonts', '//fonts.googleapis.com/css?family=Lora:400,400italic,700,700italic|Montserrat:400,700|Maven+Pro:400,700');
// Add slider CSS only if is front page ans slider is enabled
if( ( is_home() || is_front_page() ) && get_theme_mod('activello_featured_hide') == 1 ) {
wp_enqueue_style( 'flexslider-css', get_template_directory_uri().'/inc/css/flexslider.css' );
}
// Add main theme stylesheet
wp_enqueue_style( 'activello-style', get_stylesheet_uri() );
// Add Modernizr for better HTML5 and CSS3 support
wp_enqueue_script('activello-modernizr', get_template_directory_uri().'/inc/js/modernizr.min.js', array('jquery') );
// Add Bootstrap default JS
wp_enqueue_script('activello-bootstrapjs', get_template_directory_uri().'/inc/js/bootstrap.min.js', array('jquery') );
// Add slider JS only if is front page ans slider is enabled
if( ( is_home() || is_front_page() ) && get_theme_mod('activello_featured_hide') == 1 ) {
wp_register_script( 'flexslider-js', get_template_directory_uri() . '/inc/js/flexslider.min.js', array('jquery'), '20140222', true );
}
// Main theme related functions
wp_enqueue_script( 'activello-functions', get_template_directory_uri() . '/inc/js/functions.min.js', array('jquery') );
// This one is for accessibility
wp_enqueue_script( 'activello-skip-link-focus-fix', get_template_directory_uri() . '/inc/js/skip-link-focus-fix.js', array(), '20140222', true );
// Add instafeed/instagram
if( is_active_widget( false, false, 'activello-instagram', true ) ){
wp_enqueue_script('activello-instafeedjs', get_template_directory_uri().'/inc/js/instafeed.min.js', array('jquery') );
}
// Threaded comments
if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
wp_enqueue_script( 'comment-reply' );
}
}
add_action( 'wp_enqueue_scripts', 'activello_scripts' );
/**
* Custom template tags for this theme.
*/
require get_template_directory() . '/inc/template-tags.php';
/**
* Custom functions that act independently of the theme templates.
*/
require get_template_directory() . '/inc/extras.php';
/**
* Customizer additions.
*/
require get_template_directory() . '/inc/customizer.php';
/**
* Load Jetpack compatibility file.
*/
require get_template_directory() . '/inc/jetpack.php';
/**
* Load custom nav walker
*/
require get_template_directory() . '/inc/navwalker.php';
/**
* Load custom metabox
*/
require get_template_directory() . '/inc/metaboxes.php';
/**
* Social Nav Menu
*/
require get_template_directory() . '/inc/socialnav.php';
/* Globals */
global $site_layout, $header_show;
$site_layout = array('pull-right' => esc_html__('Left Sidebar','activello'), 'side-right' => esc_html__('Right Sidebar','activello'), 'no-sidebar' => esc_html__('No Sidebar','activello'),'full-width' => esc_html__('Full Width', 'activello'));
$header_show = array(
'logo-only' => __('Logo Only', 'travelify'),
'logo-text' => __('Logo + Tagline', 'travelify'),
'title-only' => __('Title Only', 'travelify'),
'title-text' => __('Title + Tagline', 'travelify')
);
/* Get Single Post Category */
function get_single_category($post_id){
if( !$post_id )
return '';
$post_categories = wp_get_post_categories( $post_id );
if( !empty( $post_categories ) ){
return wp_list_categories('echo=0&title_li=&show_count=0&include='.$post_categories[0]);
}
return '';
}
// Change what's hidden by default
add_filter('default_hidden_meta_boxes', 'be_hidden_meta_boxes', 10, 2);
function be_hidden_meta_boxes($hidden, $screen) {
if ( 'post' == $screen->base || 'page' == $screen->base ) {
// removed 'postexcerpt',
$hidden = array(
'slugdiv',
'trackbacksdiv',
'postcustom',
'commentstatusdiv',
'commentsdiv',
'authordiv',
'revisionsdiv'
);
}
return $hidden;
}
if ( ! function_exists( 'activello_woo_setup' ) ) :
/**
* Sets up theme defaults and registers support for various WordPress features.
*/
function activello_woo_setup() {
/*
* Enable support for WooCemmerce.
*/
add_theme_support( 'woocommerce' );
}
endif; // activello_woo_setup
add_action( 'after_setup_theme', 'activello_woo_setup' );
Did you try to use regular if wrapping?
/**
* Sets up theme defaults and registers support for various WordPress features.
*/
add_action( 'after_setup_theme', 'activello_woo_setup' );
if ( !function_exists( 'activello_woo_setup' ) ){
function activello_woo_setup() {
/* Enable support for WooCommerce. */
add_theme_support( 'woocommerce' );
}
}
Doubt it would make any difference, but you never know...
on line 281 : if ( ! function_exists( 'activello_woo_setup' ) ) {
replace : if ( !function_exists( 'activello_woo_setup' ) ) {
What action or filter can I use in a Wordpress plugin to dynamically replace the contents (i.e. not the header or footer) of a 404 error page?
Basically I'm looking for a 404 error page equivalent for the_content filter, which will filter the contents of an existing page.
Thank you for your time.
Note: I know I can manually modify the 404 error page for the current theme, but that is not the effect I am trying to achieve.
From this WordPress Answer: How to control output of custom post type without modifying theme?
Plugin file:
<?php
/*
Plugin Name: Plugin 404 Page
Plugin URI: http://stackoverflow.com/questions/14539884
Description: Use the plugin's template file to render a custom 404.php
Author: brasofilo
Author URI: https://wordpress.stackexchange.com/users/12615/brasofilo
Version: 2013.26.01
License: GPLv2
*/
class Universal_Template
{
public function __construct()
{
$this->url = plugins_url( '', __FILE__ );
$this->path = plugin_dir_path( __FILE__ );
add_action( 'init', array( $this, 'init' ) );
}
public function init()
{
add_filter( 'template_include', array( $this, 'template_404' ) );
}
public function template_404( $template )
{
if ( is_404() )
$template = $this->path . '/404.php';
return $template;
}
}
$so_14539884 = new Universal_Template();
And in the plugin folder, a file named 404.php:
<?php
/**
* The template for displaying 404 pages (Not Found).
*
* #package WordPress
* #subpackage Twenty_Twelve
* #since Twenty Twelve 1.0
*/
get_header(); ?>
<div id="primary" class="site-content">
MY 404!
</div><!-- #primary -->
<?php get_footer(); ?>
The solution depends on the content of 404.php file. If this file contains static text, like
_e( 'It seems we can’t find what you’re looking for...', 'twentyeleven' );
you can add your own filter
apply_filters( 'my_404_content', 'Default 404 message' );
and in functions.php (or in plugin)
add_filter( 'my_404_content', 'replace_404_message' );
function replace_404_message($message) {
return 'Error 404 - '.$message;
}
If 404.php uses built-in WP functions to display page content, you should check what filters they are supported.
You might be able to add a the_content filter with a conditional is_404 section:
function content_404($content) {
if (is_404()) {
// do some stuff with $content
}
// no matter what,
return $content;
}
add_filter( 'the_content', 'content_404' );
Note that this does assume that the 404.php page template has a the_content template tag in place.