I have some functions to override in my child theme but keep on getting Cannot redeclare langs_content().
I used quite a few resources to troubleshoot and something that shoud be straight forward I can't work it out
<?php
function enqueue_parent_theme_style() {
wp_enqueue_style( 'parent-style', get_template_directory_uri().'/style.css' );
}
add_action( 'wp_enqueue_scripts', 'enqueue_parent_theme_style' );
function enqueue_child_theme_style() {
wp_enqueue_style( 'child-style', get_stylesheet_directory_uri().'/style.css' );
}
//require_once( get_stylesheet_directory() . '/functions.php');
add_action( 'wp_enqueue_scripts', 'enqueue_child_theme_style', 100 );
if ( ! function_exists( 'langs_content' ) ) {
/*
* My Awesome function is awesome
*
*/
function langs_content( $atts, $content = null ) // shortcode lang
{
$lang = the_lang();
extract( shortcode_atts( array(), $atts ) );
$langs_content = '';
if(in_array($lang, $atts))
{
$langs_content .= do_shortcode($content);
}
return $langs_content;
}
add_shortcode('lang', 'langs_content');
}
?>
Really simple ! Remove the shortcode from init hook then add your new one ;-)
Like this :
/******
* Inside parent functions.php (don't touch this!)
******/
function test_shortcode() {
return "Hello World from parent theme";
}
add_shortcode('test_shortcode', 'test_shortcode');
/******
* Inside child functions.php
******/
function child_test_shortcode()
{
return "Hello World from child theme";
}
function remove_parent_theme_shortcodes()
{
remove_shortcode('test_shortcode');
// Add our new function with the same shortcode
add_shortcode('test_shortcode', 'child_test_shortcode');
}
// Remove the shortcode with init hook
add_action('init', 'remove_parent_theme_shortcodes');
Related
I'm trying out a plugin test where I've created a simple plugin that will wrap text in a <span> element with a class, when I in the editor wrap the text in a shortcode.
It works. But the css styling in the style.css file for that plugin isn't applied.
The plugin script is this:
class shs_wrap {
/*Create shortcode [wrap]*/
function shs_wrap() {
add_shortcode( 'wrap', array( &$this, 'shortcode' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'style' ) );
}
/*Run the shortcode*/
function shortcode( $atts , $content = null) {
/*If the "class" is forgotten in the shortcode, abort*/
if( empty(preg_quote($atts['class'])) || empty($content))
return;
/*Prepare output*/
$output = "<span class='".$atts['class']."'>" . $content . "</span>";
return $output;
}
/*Add styling from the css-file*/
function style() {
wp_register_style( 'style', plugins_url( 'style.css', __FILE__ ));
wp_enqueue_style( 'style' );
}
}
new shs_wrap();
When writing
Here is some text [wrap class="test"]here is some wrapped text[/wrap] here is some more text
I simply want the output to be:
Here is some text <span class="test">here is some wrapped text</span> here is some more text
My script above works and I can see in the source code that the <span> element is correctly added with its class - here is a screenclip from the inspection tool:
The css styling from the style.css file is not loaded, though. The output on the screen is not styled and the style file is never added. The style file is located at the same location as the php-file (called shs-wrap.php):
and it contains only this tiny css snippet:
What is the issue here? Is there an error in my style enqueue function? Do I call it wrongly?
May just be me but I think you should use __construct like below in addition to a more unique style handle:
class shs_wrap {
/*Create shortcode [wrap]*/
function __construct() {
add_shortcode( 'wrap', array( $this, 'shortcode' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'style' ) );
}
/*Run the shortcode*/
function shortcode( $atts , $content = null) {
/*If the "class" is forgotten in the shortcode, abort*/
if( empty(preg_quote($atts['class'])) || empty($content)) {
return;
}
/*Prepare output*/
$output = "<span class='".$atts['class']."'>" . $content . "</span>";
return $output;
}
/*Add styling from the css-file*/
function style() {
wp_register_style( 'my-plugin-test-style', plugins_url( 'style.css', __FILE__ ));
wp_enqueue_style( 'my-plugin-test-style' );
}
}
new shs_wrap();
I've tried the following code, which im using successfully with product thumbnails but for categories haven't found a way to do it.
add_action( 'init', 'mw_replace_woocommerce_placeholders' );
function mw_replace_woocommerce_placeholders() {
add_filter('woocommerce_placeholder_img', 'custom_woocommerce_placeholder_img');
function custom_woocommerce_placeholder_img( $src ) {
$src = '<span class="thumb-placeholder"><i class="icon-camera"></i></span>';
return $src;
}
}
Any tips?
This should work
add_action( 'init', 'custom_placeholder' );
function custom_placeholder() {
if ( is_product_category() ){
add_filter('woocommerce_placeholder_img_src','custom_placeholder_img');
function custom_placeholder_img($src) {
$upload_dir = wp_upload_dir();
$uploads = untrailingslashit( $upload_dir['baseurl'] );
$src = $uploads . '/your/directory/custom_placeholder.jpg';
return $src;
}
}
}
Or you could replace the placeholder located at "wp-content/plugins/woocommerce/assets/images/placeholder.png" with your own.
The line:
if ( is_product_category() ){
in functions.php makes the theme reliant on the plugin, so if you deactivate woocommerce for testing, it breaks the site. At least that's what happened to me.
Why can't use conditonal tags inside plugin? Here's the script:
/* enqueue */
function portfoliodetail_enqueue() {
wp_enqueue_script( 'portfoliodetailjs', plugin_dir_url(__FILE__) .'js/portfolio-detail.js', array('velocity'), null, true );
}
if ( is_singular( 'portfolio' ) ) {
add_action( 'wp_enqueue_scripts', 'portfoliodetail_enqueue' );
}
/* /enqueue */
the is_singular( 'portfolio' ) (because the post type is portfolio) works well in theme directory file. But when it is written within plugin, it doesn't work.
Anybody willing to help?
Your condition should be in the hook.
it should be like this:
function portfoliodetail_enqueue() {
if ( is_singular( 'portfolio' ) ) {
wp_enqueue_script( 'portfoliodetailjs', plugin_dir_url(__FILE__) .'js/portfolio-detail.js', array('velocity'), null, true );
}
}
add_action( 'wp_enqueue_scripts', 'portfoliodetail_enqueue' );
I have two categories of posts. I only want one to show up on a blog page. The page is not the home page. I tried using this code in the codex:
<?php
if ( is_page('page_slug') ) {
query_posts( 'cat=-(category id)' );
}
?>
to no avail. I also tried creating a new template for the page, then using
<?php
if ( is_page_template('new_blog') ) {
query_posts( 'cat=-(category id)' );
}
?>
on the index.php, and still nothing. Maybe I'm putting the code in the wrong place? Can anyone offer any advice on how I might achieve this?
EDIT:
Here is the functions.php file:
<?php
include_once get_template_directory() . '/functions/blackbird-functions.php';
$functions_path = get_template_directory() . '/functions/';
/* These files build out the options interface. Likely won't need to edit these. */
require_once ($functions_path . 'admin-functions.php'); // Custom functions and plugins
require_once ($functions_path . 'admin-interface.php'); // Admin Interfaces (options,framework, seo)
/* These files build out the theme specific options and associated functions. */
require_once ($functions_path . 'theme-options.php'); // Options panel settings and custom settings
require_once ($functions_path . 'shortcodes.php');
?>
<?php
/* ----------------------------------------------------------------------------------- */
/* Styles Enqueue */
/* ----------------------------------------------------------------------------------- */
function blackbird_add_stylesheet() {
wp_enqueue_style('shortcodes', get_template_directory_uri() . "/css/shortcode.css", '', '', 'all');
}
add_action('init', 'blackbird_add_stylesheet');
/* ----------------------------------------------------------------------------------- */
/* jQuery Enqueue */
/* ----------------------------------------------------------------------------------- */
function blackbird_wp_enqueue_scripts() {
if (!is_admin()) {
wp_enqueue_script('jquery');
wp_enqueue_script('blackbird-ddsmoothmenu', get_template_directory_uri() . '/js/ddsmoothmenu.js', array('jquery'));
wp_enqueue_script('blckbird-flex-slider', get_template_directory_uri() . '/js/jquery.flexslider-min.js', array('jquery'));
wp_enqueue_script('blackbird-testimonial', get_template_directory_uri() . '/js/slides.min.jquery.js', array('jquery'));
wp_enqueue_script('blackbird-prettyphoto', get_template_directory_uri() . '/js/jquery.prettyPhoto.js', array('jquery'));
wp_enqueue_script('blackbird-validate', get_template_directory_uri() . '/js/jquery.validate.min.js', array('jquery'));
wp_enqueue_script('blackbird-custom', get_template_directory_uri() . '/js/custom.js', array('jquery'));
} elseif (is_admin()) {
}
}
add_action('wp_enqueue_scripts', 'blackbird_wp_enqueue_scripts');
/* ----------------------------------------------------------------------------------- */
/* Custom Jqueries Enqueue */
/* ----------------------------------------------------------------------------------- */
function blackbird_custom_jquery() {
wp_enqueue_script('mobile-menu', get_template_directory_uri() . "/js/mobile-menu.js", array('jquery'));
}
add_action('wp_footer', 'blackbird_custom_jquery');
//Front Page Rename
$get_status = blackbird_get_option('re_nm');
$get_file_ac = get_template_directory() . '/front-page.php';
$get_file_dl = get_template_directory() . '/front-page-hold.php';
//True Part
if ($get_status === 'off' && file_exists($get_file_ac)) {
rename("$get_file_ac", "$get_file_dl");
}
//False Part
if ($get_status === 'on' && file_exists($get_file_dl)) {
rename("$get_file_dl", "$get_file_ac");
}
//
function blackbird_get_option($name) {
$options = get_option('blackbird_options');
if (isset($options[$name]))
return $options[$name];
}
//
function blackbird_update_option($name, $value) {
$options = get_option('blackbird_options');
$options[$name] = $value;
return update_option('blackbird_options', $options);
}
//
function blackbird_delete_option($name) {
$options = get_option('blackbird_options');
unset($options[$name]);
return update_option('blackbird_options', $options);
}
//Enqueue comment thread js
function blackbird_enqueue_scripts() {
if (is_singular() and get_site_option('thread_comments')) {
wp_print_scripts('comment-reply');
}
}
add_action('wp_enqueue_scripts', 'blackbird_enqueue_scripts');
?>
<?php
add_action('init','posts_of_one_cat');
function posts_of_one_cat() {
if ( is_page('26') ) {
query_posts( 'cat=-12' ); // 3 is id of your category you want to exclude
// do anything here
}
}
?>
get_posts() is desirable as an option.
<?php $args = array(
'category' => '-(category id)',
?>
<?php
$postslist = get_posts($args);
foreach ($postslist as $post) :
setup_postdata($post);
?>
On index.php this code will not execute as index.php is homepage add this code in functions.php in some function
something like ...
<?php
add_action('init','posts_of_one_cat');
function posts_of_one_cat() {
if ( is_page('your blog page id here') ) {
query_posts( 'cat=-3' ); // 3 is id of your category you want to exclude
// do anything here
}
}
?>
i created a custom page named custom_page.php in wordpress and use this as a page template.
i wanted to create my own functions and only target my custom_page.php. this function will be installed as a plugin.
what kind of WP HOOKS should i use, for example i have this code below
function your_function() {
if ( is_page_template('custom_page.php') ) {
echo '<p>This is inserted at the bottom</p>';
{
}
add_action('wp_footer', 'your_function');
i want to only execute this code in my custom_page.php footer area.
[EDIT]
You can use these hooks for page templates.
http://adambrown.info/p/wp_hooks/hook/page_template
Or may be something like this...
add_filter( 'page_template', 'check_the_template' );
function check_the_template( $template ) {
if( 'custom_page.php' == basename( $template ) ) {
// do some things here by calling functions, hooks etc..
}
return $template;
}
If you want to just echo "Hello World" :
add_filter( 'page_template', 'check_the_template' );
function check_the_template( $template ) {
if( 'pagetemplete.php' == basename( $template ) ) {
add_action( 'the_content', 'echofunction' );
}
return $template;
}
function echofunction() {
echo "Hello World";
}