Here I want to have upon activation of my wordpress plugins activation
Before Activation
Activate | Edit | Delete
After Activation
Settings | Edit | Delete
How can this be done in code to add this Menu?
I personally use the following snippet of code to add new action links. I found this elsewhere and modified as needed.
function my_plugin_admin_action_links($links, $file) {
static $my_plugin;
if (!$my_plugin) {
$my_plugin = plugin_basename(__FILE__);
}
if ($file == $my_plugin) {
$settings_link = 'Settings';
array_unshift($links, $settings_link);
}
return $links;
}
add_filter('plugin_action_links', 'my_plugin_admin_action_links', 10, 2);
There's a filter for plugin_action_links that you can set specifically for your plugin to add action links for your plugin on the Plugins page
Check out these blogs for more detail:
http://adambrown.info/p/wp_hooks/hook/%7B$prefix%7Dplugin_action_links
http://www.wpmods.com/adding-plugin-action-links/
There are two types of links in plugin list.Taken from
http://atiblog.com/wordpress-plugin-development/
Use the following code in your Class.
For Type 1:
add_action( 'plugin_action_links_' . plugin_basename( FILE ),array($this,'plugin_links') );
function plugin_links( $links ) {
$links = array_merge( array('' . __( 'Settings', 'textdomain' ) . ''), $links );
return $links;
}
For Type 2 : use filter.
add_filter( 'plugin_row_meta', array($this,'plugin_row_meta_links'), 10, 2 );
function plugin_row_meta_links( $links, $file ) {
$base = plugin_basename( FILE );
if ($file == $base ) {
$new_links = array(
'donate' => 'Donate',
'doc' => 'Documentation'
);
$links = array_merge( $links, $new_links ); }
return $links;
}
Related
So today, all my websites were updated to the new release of WordPress 5.9.1. Good good. However, my custom blocks in Gutenberg that are containing an image element are breaking the style of the media modal (where you can add an image directly in the post).
I started a new project, just to test if it was my theme, or the plugins, but even without any plugin (except ACF Pro) and on the Twenty Twenty-Two theme, if I add my registration code in the functions.php file of 2022 theme, I get the same problem.
Here's the register-block code:
add_action('acf/init', 'my_acf_init_block_types');
function my_acf_init_block_types() {
if( function_exists('acf_register_block_type') ) {
acf_register_block_type(array(
'name' => 'carousel',
'title' => __('Carrousel'),
'description' => __(''),
'render_template' => 'web/blocks/carousel.php',
'category' => 'custom-blocks',
'icon' => 'images-alt',
'keywords' => array( 'carousel', 'carrousel'),
'supports' => array( 'anchor' => true),
));
}
}
And I've created a Field group trying the image with the array annnnnd the one using only the URL.
What I tried:
no plugins (except ACF)
WP theme (2022)
custom theme with no functions
adding the registration code to 2022 theme (same error)
Please, help a sister our here.
I think it was cause by the 5.9.1 update
You can use this in functions.php as temporary fix
function fix_media_views_css() {
echo '<link rel="stylesheet" id="fix-media-views-css" href="'.get_bloginfo('url').'/wp-includes/css/media-views.min.css?ver=5.9.1" media="all">';
}
add_action('admin_footer', 'fix_media_views_css');
I've added that piece of code to my functions.php file (at the end, no biggy).
function acf_filter_rest_api_preload_paths( $preload_paths ) {
if ( ! get_the_ID() ) {
return $preload_paths;
}
$remove_path = '/wp/v2/' . get_post_type() . 's/' . get_the_ID() . '?context=edit';
$v1 = array_filter(
$preload_paths,
function( $url ) use ( $remove_path ) {
return $url !== $remove_path;
}
);
$remove_path = '/wp/v2/' . get_post_type() . 's/' . get_the_ID() . '/autosaves?context=edit';
return array_filter(
$v1,
function( $url ) use ( $remove_path ) {
return $url !== $remove_path;
}
);
}
add_filter( 'block_editor_rest_api_preload_paths', 'acf_filter_rest_api_preload_paths', 10, 1 );
It works perfectly like before. I've tried to downversion it to 5.9 and it worked as well, but it takes more time/effort and many mistakes can happen.
Hope it helps more than one.
ACF is aware of the issue: https://github.com/AdvancedCustomFields/acf/issues/612
Here is the temp fix, paste in your functions.php:
function acf_filter_rest_api_preload_paths( $preload_paths ) {
global $post;
$rest_path = rest_get_route_for_post( $post );
$remove_paths = array(
add_query_arg( 'context', 'edit', $rest_path ),
sprintf( '%s/autosaves?context=edit', $rest_path ),
);
return array_filter(
$preload_paths,
function( $url ) use ( $remove_paths ) {
return ! in_array( $url, $remove_paths, true );
}
);
}
add_filter( 'block_editor_rest_api_preload_paths', 'acf_filter_rest_api_preload_paths', 10, 1 );
I have develop a custom template for woocommerce checkout page and added all templates that I need to change specifically order_review.php in plugin/woocommerce/checkout/order_review.php and on order page it works perfect.
From Order page I can remove some products or add products through ajax and here is my ajax code.
ob_start();
woocommerce_order_review();
$woocommerce_order_review = ob_get_clean();
$response = array(
'cart_total' => WC()->cart->total,
'cart_item_key' => $new_key,
'fragments' => apply_filters(
'woocommerce_update_order_review_fragments',
array(
'.woocommerce-checkout-review-order-table' => $woocommerce_order_review,
)
),
);
if ( ! empty( $data ) ) {
$response['cartflows_data'] = $data;
}
return $response;
And the woocommerce_order_review(); loads woocommerce default template instead of template from my plugin.
WooCommerce does not look in plugin folders for templates by default. You need to use the filter woocommerce_locate_template in your plugin to tell WooCommerce to look in your plugin folder for templates. The solution is outlined by SkyVerge in this blog post: https://www.skyverge.com/blog/override-woocommerce-template-file-within-a-plugin/
I am including the solution here in case the post is every removed for some reason:
function myplugin_plugin_path() {
// gets the absolute path to this plugin directory
return untrailingslashit( plugin_dir_path( __FILE__ ) );
}
add_filter( 'woocommerce_locate_template', 'myplugin_woocommerce_locate_template', 10, 3 );
function myplugin_woocommerce_locate_template( $template, $template_name, $template_path ) {
global $woocommerce;
$_template = $template;
if ( ! $template_path ) $template_path = $woocommerce->template_url;
$plugin_path = myplugin_plugin_path() . '/woocommerce/';
// Look within passed path within the theme - this is priority
$template = locate_template(
array(
$template_path . $template_name,
$template_name
)
);
// Modification: Get the template from this plugin, if it exists
if ( ! $template && file_exists( $plugin_path . $template_name ) )
$template = $plugin_path . $template_name;
// Use default template
if ( ! $template )
$template = $_template;
// Return what we found
return $template;
}
You have to override WooCommerce Template
woocommerce/templates/checkout/review-order.php and Put this file in your theme folder themes/yourthemename/woocommerce/checkout/review-order.php
Hi WooCommerce ninjas!
I'm developing WooCommerce plugin and every time when I submit form (Save Changes button) on top shows update notice with 'Your settings have been saved.'. How I can hide or change this notice, I'm use woocommerce_show_admin_notice filter but don't work in my plugin class. Below is part of code from my plugin. Any ideas for hook who will be usefull?
Great thanks!
<?php
class name_of_plugin extends WC_Payment_Gateway {
public $error = '';
// Setup our Gateway's id, description and other values
function __construct() {
$this->id = "name_of_plugin";
$this->method_title = __( "Name", 'domain' );
$this->method_description = __( "Gateway Plug-in for WooCommerce", 'domain' );
$this->title = __( "TFS VPOS", 'domain' );
$this->icon = null;
$this->has_fields = false;
$this->init_settings();
$this->init_form_fields();
$this->title = $this->get_option( 'title' );
$this->testurl = 'https://example.com/payment/api';
$this->liveurl = 'https://example.com/payment/api';
// Save settings
if ( is_admin()) {
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
}
// Disable Admin Notice
add_filter( 'woocommerce_show_admin_notice', array( $this, 'shapeSpace_custom_admin_notice' ), 10, 2 );
// add the filter
add_filter( 'woocommerce_add_error', array( $this, 'filter_woocommerce_add_notice_type' ), 10, 1 );
} // End __construct()
// display custom admin notice
public function filter_woocommerce_add_notice_type($true, $notice ) {
// magic happen here...
return $true;
}
I can't see any proper hook that can be used to remove this.
But these 2 seems to work.
My first thought is to reload the page so the settings text will be gone at the time. Something like this.
add_action( 'woocommerce_sections_checkout', 'woocommerce_sections_checkout' );
function woocommerce_sections_checkout() {
if ( isset( $_GET['section'] ) && isset( $_REQUEST['_wpnonce'] ) && ( $_GET['section'] === 'paypal' )
&& wp_verify_nonce( $_REQUEST['_wpnonce'], 'woocommerce-settings' ) ) {
WC_Admin_Settings::add_message( __( 'Your settings have been saved!', 'woocommerce' ) );
wp_safe_redirect( wp_get_raw_referer() );
exit();
}
}
Then my second option is if you wanted to change the text, we can use the filter gettext.
add_filter( 'gettext', 'woocommerce_save_settings_text', 20, 3 );
function woocommerce_save_settings_text( $translated_text, $text, $domain ) {
if ( ( $domain == 'woocommerce' ) && isset( $_GET['section'] ) && ( $_GET['section'] === 'paypal' ) ) {
switch ( $translated_text ) {
case 'Your settings have been saved.' :
$translated_text = __( 'Your awesome settings have been saved.', 'woocommerce' );
break;
}
}
return $translated_text;
}
Please do note that this code is just an example and will work for paypal settings. Change everything that is needed.
I'm working on building my first plugin for wordpress and am needing it to dynamically add a custom page for a login screen among other things.
The only thing I've been able to find that's anywhere near what I'm needing is here: WP - Use file in plugin directory as custom Page Template? & Possible to add Custom Template Page in a WP plugin?, but they're still not quite what I'm looking for.
Here is the code that I currently have running in my plugin...
// Add callback to admin menu
add_action( 'template_redirect', 'uploadr_redirect' );
// Callback to add menu items
function uploadr_redirect() {
global $wp;
$plugindir = dirname( __FILE__ );
// A Specific Custom Post Type
if ( $wp->query_vars["post_type"] == 'uploadr' ) {
$templatefilename = 'custom-uploadr.php';
if ( file_exists( TEMPLATEPATH . '/' . $templatefilename )) {
$return_template = TEMPLATEPATH . '/' . $templatefilename;
} else {
$return_template = $plugindir . '/themefiles/' . $templatefilename;
}
do_theme_redirect( $return_template );
}
}
function do_theme_redirect( $url ) {
global $post, $wp_query;
if ( have_posts ()) {
include( $url );
die();
} else {
$wp_query->is_404 = true;
}
}
Using this would require that my client create new page... what I'm needing is for the pluging to auto create a custom page (with a customized path, meaning .com/custompathhere) using a template file from the plugin folder, which will then contain all actions the plugin performs.
Note: This plugin is designed to run on one page, therefore reducing load-time and etc.
Thanks in advance!
Here is my code solution for adding page templates from a Wordpress plugin (inspired by Tom McFarlin).
This is designed for a plugin (the template files are searched for in the root directory of the plugin). These files are also in exactly the same format as if they were to be included directly in a theme. This can be changed if desired - check out my full tutorial http://www.wpexplorer.com/wordpress-page-templates-plugin/ for greater detail on this solution.
To customise, simply edit the following code block within the __construct method;
$this->templates = array(
'goodtobebad-template.php' => 'It\'s Good to Be Bad',
);
Full code;
class PageTemplater {
/**
* A Unique Identifier
*/
protected $plugin_slug;
/**
* A reference to an instance of this class.
*/
private static $instance;
/**
* The array of templates that this plugin tracks.
*/
protected $templates;
/**
* Returns an instance of this class.
*/
public static function get_instance() {
if( null == self::$instance ) {
self::$instance = new PageTemplater();
}
return self::$instance;
}
/**
* Initializes the plugin by setting filters and administration functions.
*/
private function __construct() {
$this->templates = array();
// Add a filter to the attributes metabox to inject template into the cache.
add_filter(
'page_attributes_dropdown_pages_args',
array( $this, 'register_project_templates' )
);
// Add a filter to the save post to inject out template into the page cache
add_filter(
'wp_insert_post_data',
array( $this, 'register_project_templates' )
);
// Add a filter to the template include to determine if the page has our
// template assigned and return it's path
add_filter(
'template_include',
array( $this, 'view_project_template')
);
// Add your templates to this array.
$this->templates = array(
'goodtobebad-template.php' => 'It\'s Good to Be Bad',
);
}
/**
* Adds our template to the pages cache in order to trick WordPress
* into thinking the template file exists where it doens't really exist.
*
*/
public function register_project_templates( $atts ) {
// Create the key used for the themes cache
$cache_key = 'page_templates-' . md5( get_theme_root() . '/' . get_stylesheet() );
// Retrieve the cache list.
// If it doesn't exist, or it's empty prepare an array
$templates = wp_get_theme()->get_page_templates();
if ( empty( $templates ) ) {
$templates = array();
}
// New cache, therefore remove the old one
wp_cache_delete( $cache_key , 'themes');
// Now add our template to the list of templates by merging our templates
// with the existing templates array from the cache.
$templates = array_merge( $templates, $this->templates );
// Add the modified cache to allow WordPress to pick it up for listing
// available templates
wp_cache_add( $cache_key, $templates, 'themes', 1800 );
return $atts;
}
/**
* Checks if the template is assigned to the page
*/
public function view_project_template( $template ) {
global $post;
if (!isset($this->templates[get_post_meta(
$post->ID, '_wp_page_template', true
)] ) ) {
return $template;
}
$file = plugin_dir_path(__FILE__). get_post_meta(
$post->ID, '_wp_page_template', true
);
// Just to be safe, we check if the file exist first
if( file_exists( $file ) ) {
return $file;
}
else { echo $file; }
return $template;
}
}
add_action( 'plugins_loaded', array( 'PageTemplater', 'get_instance' ) );
Check out my tutorial on this for more info.
http://www.wpexplorer.com/wordpress-page-templates-plugin/
I hope this helps you in what you want to do :)
I actually was able to talk to a developer friend of mine after revising the code quite a bit.
Here it is...
<?php
register_activation_hook( __FILE__, 'create_uploadr_page' );
function create_uploadr_page() {
$post_id = -1;
// Setup custom vars
$author_id = 1;
$slug = 'event-photo-uploader';
$title = 'Event Photo Uploader';
// Check if page exists, if not create it
if ( null == get_page_by_title( $title )) {
$uploader_page = array(
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_author' => $author_id,
'post_name' => $slug,
'post_title' => $title,
'post_status' => 'publish',
'post_type' => 'page'
);
$post_id = wp_insert_post( $uploader_page );
if ( !$post_id ) {
wp_die( 'Error creating template page' );
} else {
update_post_meta( $post_id, '_wp_page_template', 'custom-uploadr.php' );
}
} // end check if
}
add_action( 'template_include', 'uploadr_redirect' );
function uploadr_redirect( $template ) {
$plugindir = dirname( __FILE__ );
if ( is_page_template( 'custom-uploadr.php' )) {
$template = $plugindir . '/templates/custom-uploadr.php';
}
return $template;
}
?>
I'm providing a general solution for those that want to add a template to a post from the their plugin. Use the single_template filter.
<?php
add_filter( 'single_template', 'add_custom_single_template', 99 );
function add_custom_single_template( $template ) {
return plugin_dir_path( __FILE__ ) . 'path-to-page-template-inside-plugin.php';
}
?>
Also, if you want to use the template in a specific post type, then:
<?php
add_filter( 'single_template', 'add_custom_single_template', 99 );
function add_custom_single_template( $template ) {
if ( get_post_type() == 'post-type-name'; ) {
return plugin_dir_path( __FILE__ ) . 'path-to-page-template-inside-plugin.php';
}
return $template;
}
?>
I'm trying to add a custom default avatar to WordPress in functions.php, but the image is not displaying in Settings/Discussion or elsewhere on the site. The code works because a new radio field is added with the custom field name, but the image won't display. Is the avatar not displaying because I'm using Localhost?
I don't have enough reps to comment on similar questions.
here's the code:
add_filter( 'avatar_defaults' , 'wps_new_avatar' );
function wps_new_avatar( $avatar_defaults ){
$new_avatar = get_stylesheet_directory_uri() . '/images/default-avatar.png';
$avatar_defaults[$new_avatar] = "Default Avatar";
return $avatar_defaults;
}
I've tried other examples and the 'Add-New-Default-Avatar' plugin with the same result.
I was facing the same issue and came up with this completely hackish solution... It works though :)
add_filter( 'get_avatar', 'so_14088040_localhost_avatar', 10, 5 );
function so_14088040_localhost_avatar( $avatar, $id_or_email, $size, $default, $alt )
{
$whitelist = array( 'localhost', '127.0.0.1' );
if( !in_array( $_SERVER['SERVER_ADDR'] , $whitelist ) )
return $avatar;
$doc = new DOMDocument;
$doc->loadHTML( $avatar );
$imgs = $doc->getElementsByTagName('img');
if ( $imgs->length > 0 )
{
$url = urldecode( $imgs->item(0)->getAttribute('src') );
$url2 = explode( 'd=', $url );
$url3 = explode( '&', $url2[1] );
$avatar= "<img src='{$url3[0]}' alt='' class='avatar avatar-64 photo' height='64' width='64' />";
}
return $avatar;
}
Result:
Of course, this filter is meant for development only.