Get template part in WordPress plugin - php

I'm trying to move a WordPress plugin from my child theme to a custom plugin. The current code is using get_template_part which works on the child theme but not on the custom WordPress plugin.
I have tried to replace it for "include" but it doesn't work.
function arbolesplantados_endpoint_content() {
get_template_part('arbolesplantados'); //I think the problem is here
}
add_action( 'woocommerce_account_arbolesplantados_endpoint', 'arbolesplantados_endpoint_content' );
function arbolesplantados_account_menu_items( $items ) {
$my_items = array(
'arbolesplantados' => __( 'Gestionar árboles plantados', 'woocommerce' )
);
$my_items = array_slice( $items, 0, 1, true ) +
$my_items +
array_slice( $items, 1, count( $items ), true );
return $my_items;
}
add_filter( 'woocommerce_account_menu_items', 'arbolesplantados_account_menu_items');
The previous code is supposed to create a page with the woocommerce menu (my account) on the left and then on the right print the custom form that's inside a file called arbolesplantados.php which is in the root folder of my plugin

You missed to register endpoint for the custom page. You can register endpoint like this.
function wpso_add_my_account_endpoint() {
add_rewrite_endpoint( 'arbolesplantados', EP_PAGES );
}
add_action( 'init', 'wpso_add_my_account_endpoint' );

Related

How to hide "enable auto-updates" link on Wordpress Installed Plugins page

I have modified a Wordpress plugin which should not be auto updated later. So in order to prevent any admin from mistakenly clicking the "enable auto-updates" link on the installed plugins page, I want to hide it. I have seen some of the pro/premium/nulled plugins doing this but couldn't find a workaround. How can I remove that link for a particular plugin?
Added the following code to the plugin's main php file (plugins/example-plugin/example-plugin.php) but still the link shows on the installed plugins page.
add_filter( 'http_request_args', 'dm_prevent_update_check', 10, 2 );
function dm_prevent_update_check( $r, $url ) {
if ( 0 === strpos( $url, 'http://api.wordpress.org/plugins/update-check/' ) ) {
$my_plugin = plugin_basename( __FILE__ );
$plugins = unserialize( $r['body']['plugins'] );
unset( $plugins->plugins[$my_plugin] );
unset( $plugins->active[array_search( $my_plugin, $plugins->active )] );
$r['body']['plugins'] = serialize( $plugins );
}
return $r;
}
you can remove the updates by adding this code into your functions.php file of theme.
remove_action( 'load-update-core.php', 'wp_update_plugins' );
add_filter( 'pre_site_transient_update_plugins', create_function( '$a', "return null;" ) );

On a WordPress page template, what code can customize title and meta description?

On custom-page-template.php, what code can customize the section ( and <meta name=”description”) for all pages which are based on this template?
I tried using the following but they nothing on a page template. (They work on functions.php though).
add_filter( 'document_title_parts', 'custom_document_title_parts' );
add_action( 'wp_head', 'my_add_meta_description');
Any idea, what code can customize and in a custom-page-template.php?
Thanks.
Use this in functions.php: (change page template's name in is_page_template)
add_filter( 'document_title_parts', function( $title_parts_array ) {
if ( is_page_template('custom.php') ) {
$title_parts_array['title'] = 'New title';
echo '<script>document.querySelector(\'meta[name="description"]\').setAttribute("content", "New description");</script>';
}
return $title_parts_array;
} );
Or you can use this before get_header() in your custom page template
function new_page_title() {
echo '<script>document.querySelector(\'meta[name="description"]\').setAttribute("content", "New description2");</script>';
return 'New Title2';
}
add_action( 'pre_get_document_title', 'new_page_title' );

Rename My account tabbed menu items in Woocommerce

I am currently using Wordpress Version 4.9.8 and enabled Woocommerce plugin. I need to rename the tab name "Dashboard" to "My Rewards" in My account pages.
I found the code below, but it doesn't seem to be working:
function wpb_woo_endpoint_title( $title, $id ) {
if ( is_wc_endpoint_url( 'dashboard' ) && in_the_loop() ) { // add your endpoint urls
$title = "My Rewards"; // change your entry-title
return $title;
}
add_filter( 'the_title', 'wpb_woo_endpoint_title', 10, 2 );
Any help is appreciated.
Try the following instead (Works in Woocommerce since version 2.6):
add_filter( 'woocommerce_account_menu_items', 'custom_my_account_menu_items', 22, 1 );
function custom_my_account_menu_items( $items ) {
$items['dashboard'] = __("My Rewards", "woocommerce");
return $items;
}
This code goes in function.php file of your active child theme (or active theme). Tested and works.

Wordpress dequeue scripts without handle?

I'm currently using Foobox lightbox (free) plugin, and apparently the plugins' files are loaded on every page regardless of whether it's used or not. However I would like to change this, but I can't seem to find any handles to dequeue the scripts through.
It seems that the scripts are called by the code beneath, which to me at least, doesn't show any handles. I've tried using remove_action(...) to "counter" them as well as various inputs in wp_dequeue_script() to try and target the files directly - e.g. wp_dequeue_script('foobox.free.min.js')
class Foobox_Free extends Foo_Plugin_Base_v2_1 {
const JS = 'foobox.free.min.js';
const CSS = 'foobox.free.min.css';
const CSS_NOIE7 = 'foobox.noie7.min.css';
const FOOBOX_URL = 'http://fooplugins.com/plugins/foobox/?utm_source=fooboxfreeplugin&utm_medium=fooboxfreeprolink&utm_campaign=foobox_free_pro_tab';
const BECOME_AFFILIATE_URL = 'http://fooplugins.com/affiliate-program/';
private static $instance;
public static function get_instance() {
if ( ! isset( self::$instance ) && ! ( self::$instance instanceof Foobox_Free ) ) {
self::$instance = new Foobox_Free();
}
return self::$instance;
}
/**
* Initialize the plugin by setting localization, filters, and administration functions.
*/
private function __construct() {
//init FooPluginBase
$this->init( FOOBOXFREE_FILE, FOOBOXFREE_SLUG, FOOBOX_BASE_VERSION, 'FooBox FREE' );
if (is_admin()) {
add_action('admin_head', array($this, 'admin_inline_content'));
add_action('foobox-free-settings_custom_type_render', array($this, 'custom_admin_settings_render'));
new FooBox_Free_Settings();
add_action( FOOBOX_ACTION_ADMIN_MENU_RENDER_GETTING_STARTED, array( $this, 'render_page_getting_started' ) );
add_action( FOOBOX_ACTION_ADMIN_MENU_RENDER_SETTINGS, array( $this, 'render_page_settings' ) );
add_action( 'admin_notices', array( $this, 'admin_notice_foogallery_lightboxes' ) );
add_action( 'wp_ajax_foobox_foogallery_lightboxes_ignore_notice', array( $this, 'admin_notice_foogallery_lightboxes_ignore' ) );
add_action( 'wp_ajax_foobox_foogallery_lightboxes_update', array( $this, 'admin_notice_foogallery_lightboxes_update' ) );
add_action( 'admin_print_scripts', array( $this, 'admin_notice_foogallery_lightboxes_inline_js' ), 999 );
add_filter( 'foobox-free-has_settings_page', '__return_false' );
} else {
// Render JS to the front-end pages
add_action('wp_enqueue_scripts', array($this, 'frontend_print_scripts'), 20);
add_action('foobox-free_inline_scripts', array($this, 'inline_dynamic_js'));
// Render CSS to the front-end pages
add_action('wp_enqueue_scripts', array($this, 'frontend_print_styles'));
if ( $this->is_option_checked('disable_others') ) {
add_action('wp_footer', array($this, 'disable_other_lightboxes'), 200);
}
}
}
How, if possible, do I dequeue these scripts (and styles) without editing the plugin file directly?
Edit
Below I've added the things I've tried doing to remove the scripts (all added to functions.php):
add_action( 'wp_enqueue_scripts', 'remove_foobox_scripts', 100 );
function remove_foobox_scripts() {
if ( !is_page('my-page') ) {
wp_deregister_script( 'foobox.free.min.js' );
wp_dequeue_script( 'foobox.free.min.js' );
}
}
Also tried the below, which is just a straight copy from the foobox file:
remove_action('wp_enqueue_scripts', array($this, 'frontend_print_scripts'), 20);
remove_action('foobox-free_inline_scripts', array($this, 'inline_dynamic_js'));
remove_action('wp_enqueue_scripts', array($this, 'frontend_print_styles'));
Also tried the below, where the array( part is removed:
remove_action('wp_enqueue_scripts','frontend_print_scripts', 20);
remove_action('foobox-free_inline_scripts', 'inline_dynamic_js');
remove_action('wp_enqueue_scripts', 'frontend_print_styles');
The problem with the way you are trying to do this stems from the order in which things happen in Wordpress.
You are relying on the conditional tags like is_page('my-page') to determine whether or not to load the plugin. These conditional tags do not become available until Wordpress has parsed the URL for the current query, and at this point all the plugins and your theme have already been loaded. Even if you parse the URL yourself instead of using the conditional tags you cannot be sure your code will run before the plugins are loaded.
The solution is to add your code as an mu-plugin. These are loaded before normal plugins so you can use an option (option name) filter here to alter the plugins you want to be loaded.
Option filters pass an array to your function containing the values which are set for that option, so in this case you want to hook option_active_plugins.
You can find the values to use for by running print_r(get_option('active_plugins')); or look through the plugins folder of your wordpress install.
The following example is specific to your question, but you could modify it to make a more comprehensive set of rules, adding and removing multiple plugins on different pages based on many conditions.
My function checks you are not in wp-admin and then has 2 conditions. The first disables a normally active plugin on the specified pages, the second enables a normally disabled plugin on the specified pages.
<?php
add_filter( 'option_active_plugins', 'add_or_remove_plugins' );
function add_or_remove_plugins( $plugins ) {
if (strpos( $_SERVER['REQUEST_URI'], '/wp-admin/' ) !== 0) { // Disable in admin pages or admin plugin settings stop working properly
if (strpos( $_SERVER['REQUEST_URI'], '/remove-plugin-here/' ) === 0) { // Conditonal tags still unavailable so you have to parse urls yourself
$k = array_search( 'foobox-image-lightbox/foobox-free.php', $plugins ); // This will stop an active plugin from loading
if( false !== $k ){
unset( $plugins[$k] );
}
}
if (strpos( $_SERVER['REQUEST_URI'], '/add-plugin-here/' ) === 0) {
$plugins[] = 'foobox-image-lightbox/foobox-free.php'; // This will load the plugin along with all the active plugins
}
}
return $plugins;
}
?>
To install just change the values to suit, paste into a file and upload into your mu-plugins folder
EDIT
Looks like your inline js is added to wp_head during the constructor of the Foobox_Free class. You could try adding this to your functions.php:
add_action( 'wp_head', 'remove_dynamic_js' );
function remove_dynamic_js(){
$foo = Foobox_Free::getInstance();
remove_action('wp_head', array($foo, 'inline_dynamic_js'));
}
or if that doesn't work then maybe this:
add_action( 'wp_head', 'remove_dynamic_js' );
function remove_dynamic_js(){
remove_action('wp_head', array('Foobox_Free', 'inline_dynamic_js'));
}
The action is added inside a private function, so I don't know if either of those will actually work. Give it a shot. If not my first answer will as it stops the plugin from loading at all on the specified pages.
UPDATE
Well, I was close... Here's the code to remove the scripts, as supplied by the plugin author.
$foobox = Foobox_Free::get_instance();
remove_action('foobox-free_inline_scripts', array($foobox, 'inline_dynamic_js'));

How are action links for posts added in WordPress?

Certain WordPress plugins add links to the little mini-menu that shows up when hovering over post names in the "All Posts" section. This is what I'm talking about, like Purge from cache or Clone.
How do I add my own?
you use the add_filter hook
so for example if you want a link to search google for the page title.
add this to your functions.php
function search_google($actions, $page_object)
{
$actions['google_link'] = '' . __('Search Google for Page Title') . '';
return $actions;
}
add_filter('page_row_actions', 'search_google', 10, 2);
for a Custom Post Type
add_filter('page_row_actions', 'search_google', 10, 2);
function search_google($actions, $post)
{
if ($post->post_type =="YOUR_POST_TYPE"){
$actions['google_link'] = '' . __('Search Google for Page Title') . '';
return $actions;
}
}
more examples can be found here
The old version is not working anymore.
Now use this one:
add_filter('post_row_actions', 'search_google', 10, 2);
function search_google($actions, $post) {
$actions['google_link'] = '' . __('Search Google for Page Title') . '';
return $actions;
}
And for a custom post type:
add_filter('post_row_actions', 'search_google', 10, 2);
function search_google($actions, $post) {
if ($post->post_type =="YOUR_POST_TYPE"){
$actions['google_link'] = '' . __('Search Google for Page Title') . '';
return $actions;
}
}
In my situation, I wanted to both alter the behavior of the current links by having edit & view open in a new tab and add my own link specific to the Divi Builder theme. I wanted this to apply to pages, posts and all custom post types.
Because I am applying this change to both pages and posts, I had to do both the post_row_actions and page_row_actions add_filter actions.
This is the code added to my child theme functions.php file.
/* Modify row actions */
/* Modify row actions */
/* Modify row actions */
// Open Edit in new
function edit_new_tab( $actions, $page_object ) {
$actions['edit'] = '' . __( 'Edit' ) . '';
return $actions;
}
add_filter( 'post_row_actions', 'edit_new_tab', 10, 2 );
add_filter( 'page_row_actions', 'edit_new_tab', 10, 2 );
// Open View in new
function view_new_tab( $actions, $page_object ) {
$actions['view'] = '' . __( 'View' ) . '';
return $actions;
}
add_filter( 'post_row_actions', 'view_new_tab', 10, 2 );
add_filter( 'page_row_actions', 'view_new_tab', 10, 2 );
// Divi Builder in new
function add_divi_link( $actions, $page_object ) {
$actions['add_divi_link'] = '' . __( 'Divi Builder' ) . '';
return $actions;
}
add_filter( 'post_row_actions', 'add_divi_link', 10, 2 );
add_filter( 'page_row_actions', 'add_divi_link', 10, 2 );
In this code, view in $actions['view'] matches a built in row type. Because of this, the row type view is overwritten with the respective code. This also applies to edit. To view the row types and how they appear in your instance you can inspect the source code of the page and pay attention to the <span> that surrounds the link you want to modify. The class of the <span> will be the name you will use in $actions['span_class_goes_here']. Therefore, $actions['edit'] will determine that the Edit link will be modified.
In the case of $actions['add_divi_link'], add_divi_link does not exist in my scenario so it will be added as a new row action.

Categories