php file is not overriding in wordpress child theme - wordpress

I have a coruscate premium theme. I have created a child theme for future protection. I want to override a function in file parent-theme/framework/shortcodes/widgets.php which is defined as:
function wt_shortcode_recent_posts($atts) {
//some code here
}
add_shortcode('wt_recent_posts', 'wt_shortcode_recent_posts');
Now, In my functions.php of child theme, I tried to override the function as:
function wt_shortcode_recent_posts($atts) {
//some modified code here
}
I have also tried making a file to identical directory as child-theme/framework/shortcodes/widgets.php. It also doesn't work.
What is the correct way to override those functions or php files in wordpress child theme.

You have to override the shortcode function. Add the folowing to your child theme functions.php
function some_new_shortcoe_function($atts) {
// New shortcode code here
}
remove_shortcode('wt_recent_posts');
add_shortcode('wt_recent_posts', 'some_new_shortcode_function');

Related

How I can print all variable of a hook in drupal 8?

I'm very new in Drupal 8 and I have issue now with hook. Mainly I though that I don't clearly understand structure and hook definition in Drupal 8.
So my main problem is that I have some hook to interact with main menu (add custom class name to ul, li and link, a tag). I can do it by changing template file and now try to do it with any hook.
Although I found that some hook relating to menu ex. hook_contextual_links_alter (link: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Menu%21menu.api.php/function/hook_contextual_links_alter/8.9.x).
At the end of this hook we have the code related:
function hook_contextual_links_alter(array &$links, $group, array $route_parameters) {
if ($group == 'menu') {
// Dynamically use the menu name for the title of the menu_edit contextual
// link.
$menu = \Drupal::entityTypeManager()
->getStorage('menu')
->load($route_parameters['menu']);
$links['menu_edit']['title'] = t('Edit menu: #label', [
'#label' => $menu
->label(),
]);
}
}
So I have installed devel module with kint function and in my .theme file and try:
function hook_contextual_links_alter(array &$links, $group, array $route_parameters) {
kint($links);
}
and then reload my Home page but nothing showed. But I can get some information about other like:
function eg_learn_theme_suggestions_page_alter(&$suggestions, $variables) {
kint($suggestions);
}
So what happens here? Can you help to explain if how I can print the variable of this hook (in .theme file) and the site page to see the printing variable?
In general when I found a hook, how I can print there array and check it in website?
There are some problems about your approach:
When implementing a hook, you must replace "hook" with the module name/theme name where you put the hook function inside. For example, if you want implement hook_contextual_links_alter in your_custom module, it becomes your_custom_contextual_links_alter().
Not all hook can be implemented in the theme. Some hook can only be implemented in modules (in .module file). You can read more here.
In your case, I think hook_preprocess_menu would be more suitable. You can implement it in your custom theme like this:
function <your_theme_name>_preprocess_menu(&$variables) {
if ($variables['menu_name'] == 'main') {
kint($variables);
}
}

How to set child theme path

I am trying to set the child theme path. Instead of having child theme templates loaded from the standard location I want to load them from a different location.
For example I have themes:
wp-content/themes/twentyfifteen
wp-content/themes/twentyfifteen_child
where twentyfifteen_child has style.css and functions.php along with any override templates. This is working fine, but I want to programatically set the child theme path to be this instead:
wp-content/themes/twentyfifteen_child/twentyfifteen_grandchild
Is this possible?
I just figured out the answer although there might be a better way of doing this. I tried the following in functions.php and that didn't work. But if I add the same thing in a plugin it works.
add_filter( 'stylesheet_directory', 'custom_stylesheet_directory' );
function custom_stylesheet_directory($dir)
{
$project = $dmmEnv->getProject();
$dir = str_replace(
'twentyfifteen_child' ,
'twentyfifteen_child/twentyfifteen_grandchild', $dir);
return $dir;
}

How to filter pluggable function from wordpress child-theme

I need to add a filter to a specific wordpress function which is defined in a file which is included by a pluggable function theme's functions f
functions.php:
if (!function_exists('nectar_colors_css_output')) {
function nectar_colors_css_output(){
include('css/colors.php');
}
}
colors.php:
<?php
function nectar_colors() {
// not relevant what happens here, but I have
// to call another function before this one is called!
}
?>
I use a child theme and when I try to filter this function from the child theme's functions.php, nothing happens. This is because pluggable functions of the parent theme will be called after calling the filters from the child theme.
My filter function in the child theme's functions.php is
function filter_function() {
// some custom actions...
}
add_filter('nectar_colors', 'filter_function');
How can I get this filter working?
What exactly are you trying to filter on? You might be misunderstanding the concepts.
Take your example provided. The parent checks if a function exists before creating it.
So basically, if you define it in your child's theme, the parent will know it's there and won't create a default one.
#themename-child
function nectar_colors_css_output(){
error_log("It worked!");
}

Where to put javascript function in wordpress to apply to side navigation

I have the following code to initially hide sub navigation of a sidebar menu in wordpress:
$(document).ready(function() {
$(".children").hide();
$("#menu-item").click(function() {
$('.children').slideToggle('medium');
});
});
I have it working correctly in jsfiddle(http://jsfiddle.net/MLUb8/), but can't get it working in wordpress. Where should it be placed within my theme for this to work? I've tried the header.php, footer.php and template file.
You should put the code in a separate file and include it using wp_enqueue_script (the value for $deps should be array( 'jquery' ):
http://codex.wordpress.org/Function_Reference/wp_enqueue_script
Additionally, WordPress uses noconflict, so you should slightly modify your code, like so:
jQuery(document).ready(function($) {
$(".children").hide();
$("#menu-item").click(function() {
$('.children').slideToggle('medium');
});
});
It actually worked directly within the template file itself. No need to embed from a separate file.

Unregister WordPress plugin widget from theme

Question about WordPress theme - plugins interaction.
Can i unregister widget that was added by plugin using theme functions.php file?
Tried to unregister it using sample code, but it didn't work for me:
function remove_some_widget() {
unregister_widget('some_plugin_widget');
}
add_action( 'widgets_init', 'remove_some_widget' );
The parameter to pass in unregister_widget function is the name of a class that extends, so, pass the appropriate class name. Here is an example of unregister_widget to Unregister all widgets
function unregister_default_widgets() {
unregister_widget('WP_Widget_Pages');
unregister_widget('WP_Widget_Calendar');
unregister_widget('WP_Widget_Archives');
unregister_widget('WP_Widget_Links');
unregister_widget('WP_Widget_Meta');
unregister_widget('WP_Widget_Search');
unregister_widget('WP_Widget_Text');
unregister_widget('WP_Widget_Categories');
unregister_widget('WP_Widget_Recent_Posts');
unregister_widget('WP_Widget_Recent_Comments');
unregister_widget('WP_Widget_RSS');
unregister_widget('WP_Widget_Tag_Cloud');
unregister_widget('WP_Nav_Menu_Widget');
unregister_widget('Twenty_Eleven_Ephemera_Widget');
}
add_action('widgets_init', 'unregister_default_widgets', 11);

Resources