How to add a button to a sidebar widget?

I am new to WordPress and I want to add a custom button to the admin control panel in a PointFinder theme. I know the concept of an action hook and have already successfully implemented my own action hook using do_action_ref_array() and called it from functions.php using add_action() .

Approach 1 (definition of put button statically in php file)

In the file 'dashboard-page.php', a widget of the target sidebar is built into which I want to add an additional button. Here is an excerpt from the code:

 $pfmenu_output .= ($setup11_reviewsystem_check == 1) ? '<li><a href="'.$setup4_membersettings_dashboard_link.$pfmenu_perout.'ua=reviews"><i class="pfadmicon-glyph-377"></i> '. $setup29_dashboard_contents_rev_page_menuname.'</a></li>' : ''; $pfmenu_output .= '<li><a href="http://my.testsite.com/market"><i class="pfadmicon-glyph-476"></i> '. esc_html__('Car Market','pointfindert2d').'</a></li>'; $pfmenu_output .= '<li><a href="'.esc_url(wp_logout_url( home_url() )).'"><i class="pfadmicon-glyph-476"></i> '. esc_html__('Logout','pointfindert2d').'</a></li>'; 

The second line is my static approach. The button is correctly added to the sidebar widget. But I need to put this code in the functions.php my Child-Theme in order to save it during future update procedures.

Approach 2 (more dynamic with self-defined action)

I also tried adding my own action hook instead of statically adding a button (replaced the second line with the definition of the action hook:

 $pfmenu_output .= ($setup11_reviewsystem_check == 1) ? '<li><a href="'.$setup4_membersettings_dashboard_link.$pfmenu_perout.'ua=reviews"><i class="pfadmicon-glyph-377"></i> '. $setup29_dashboard_contents_rev_page_menuname.'</a></li>' : ''; do_action_ref_array( 'pf_add_widget_button', array(&$pfmenu_output) ); $pfmenu_output .= '<li><a href="'.esc_url(wp_logout_url( home_url() )).'"><i class="pfadmicon-glyph-476"></i> '. esc_html__('Logout','pointfindert2d').'</a></li>'; 

Subsequently, I added a call to add_action() my child function.php topic and added a button there, which also works fine:

 function swi_add_button_to_widget(&$pfmenu_output) { $pfmenu_output .= '<li><a href="http://my.testsite.com/market"><i class="pfadmicon-glyph-476"></i> '. esc_html__('Car Market,'pointfindert2d').'</a></li>'; } add_action( 'pf_add_widget_button', 'swi_add_button_to_widget' ); 

Problem definition

But both approaches described above will work only until I update the PointFinder theme for the first time, since dashboard-page.php is likely to be overridden during the update.

I did not find any preliminary action hooks implemented by the theme development team by searching all files looking for do_action() and do_action_ref_array() . Nothing...

Decision?

Therefore, is there another way to access this $pfmenu_output variable from my child theme to add an extra button?

Am I completely stuck when theme developers did n't embed some pre-created action hooks for this particular purpose?

+5
source share
2 answers

Option 1: Copy dashboard-page.php to a child theme

I'm just expanding on Sebastian's comment . You can copy the entire dashboard-page.php (using your own added action) to the child themes folder. Then you have to look for a place in the parent theme, which includes the dashboard-page.php . I hope it is included in one of the source topics of the standard template files . Then you just copy this template file into your child theme to override it. Then you will have to change this line, which includes the dashboard-page.php , to include the same file in the child folder.

For example, if the page.php template page.php has something like this:

 include (get_template_directory() . '/dashboard-page.php') 

You can copy the page.php file into your child theme and change this line as follows:

 include (get_stylesheet_directory() . '/dashboard-page.php') 

Note that get_stylesheet_directory() retrieves the path to the child theme folder and get_template_directory() retrieves the path to the parent theme folder.

Important: It is possible that the dashboard-page.php not included in one of the standard template files, which can be overridden, but are instead included by other non-standard template files that are included in the parent theme. In this case, you will have to copy each included file to the dashboard-page.php . And make sure each include uses a child theme version for each file.

Option 2: Use Javascript / jQuery

One alternative approach is to use javascript code to insert the button. You can add javascript code to one of your child theme templates to dynamically change the menu after the page loads. I took a look at a demonstration of the topic, and it looks like the list item for the sidebar has a class called "pf-sidebar-menu". You can simply add the following code for this class and insert your button after the first element of the list:

 jQuery(".pf-sidebar-menu > li:nth-child(1)").after('<li><a href="http://my.testsite.com/market"><i class="pfadmicon-glyph-476"></i>Car Market</a></li>'); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <ul class="pf-sidebar-menu"> <li><a href="#"><i class="pfadmicon-glyph-377"></i>Something</a></li> <li><a href="#"><i class="pfadmicon-glyph-476"></i>Logout</a></li> </ul> 

FYI, an easy way to add the above code to your website will be as follows: child functions.php file. This creates a script tag in the footer of your site with the above code. You may need to add additional validations to target a specific page or pages.

 <?php function js_add_sidebar_button() { ?> <script type="text/javascript"> jQuery(".pf-sidebar-menu > li:nth-child(1)").after('<li><a href="http://my.testsite.com/market"><i class="pfadmicon-glyph-476"></i>Car Market</a></li>'); </script> <?php } add_action( 'wp_footer', 'js_add_sidebar_button' ); 
+4
source

use PHP Code Widget plugin

PHP Code Widget

+1
source

Source: https://habr.com/ru/post/1264461/


All Articles