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.
Related
I'm developing a plugin for custom product type. Here's my class that is being registered on plugins_loaded hook:
class WC_Product_Subscription extends WC_Product {
public function __construct( $product ) {
$this->product_type = 'subscription';
$this->purchasable = true;
$this->downloadable = false;
$this->virtual = true;
$this->sold_individually = true;
$this->manage_stock = false;
$this->supports[] = 'ajax_add_to_cart';
parent::__construct( $product );
}
public function is_purchasable() {
return true;
}
}
The problem is that I cannot see "Add to Cart" button on the product page which means my product cannot be purchased. I tried adding
public function add_to_cart_url() {
return apply_filters( 'woocommerce_product_add_to_cart_url', get_permalink( $this->get_id() ), $this );
}
public function add_to_cart_text() {
$text = $this->is_purchasable() && $this->is_in_stock() ? __( 'Add to cart', 'woocommerce' ) : __( 'Read more', 'woocommerce' );
return apply_filters( 'woocommerce_product_add_to_cart_text', $text, $this );
}
to the class but without success. I'm stuck.
It appears there are some missing steps to make your custom product type work.
Try the steps below:
#1. Make sure that your plugin is active.
#2. Make sure the product is in stock and has a price set. WooCommerce checks both of these conditions before displaying the Add to Cart button.
#3. Check if the custom product type is registered correctly. Use the following code to check:
add_action( 'init', 'check_registered_product_types' );
function check_registered_product_types() {
$product_types = wc_get_product_types();
var_dump( $product_types );
}
#4. Make sure that the WooCommerce product type is supported. Use the following code to check:
add_filter( 'product_type_selector', 'custom_product_type_selector' );
function custom_product_type_selector( $product_types ) {
var_dump( $product_types );
return $product_types;
}
#5. Make sure that the product class is correctly loaded. Use the following code to check:
add_action( 'plugins_loaded', 'check_product_class' );
function check_product_class() {
$product_class = 'WC_Product_Subscription';
var_dump( class_exists( $product_class ) );
}
#6. Ensure you have a product template for your custom product type in your theme's WooCommerce folder (e.g. single-product-subscription.php).
#7. If everything else seems to be working, you might have to override the WooCommerce templates to display the Add to Cart button.
Edit:
You can create a template in your plugin directory by using the following code:
add_filter( 'woocommerce_locate_template', 'wc_subscription_template', 10, 3 );
function wc_subscription_template( $template, $template_name, $template_path ) {
if ( 'single-product-subscription.php' === $template_name ) {
$template = untrailingslashit( plugin_dir_path( __FILE__ ) ) . '/templates/single-product-subscription.php';
}
return $template;
}
This will tell WooCommerce to use your custom template in the templates folder within your plugin directory. Make sure you put the code in a file that is included in your plugin, so it will run when the plugin is activated.
Wordpress - functions.php
I'm using this function to automatically add custom structure in url for posts in a specific categorie.
But now I would like to know how I could add multiple categories without having to copy this function over and over.
Other solution could be to have this function work for the parent post category and all child categories.
// url structure
add_filter( 'post_link', 'custom_permalink', 10, 3 );
function custom_permalink( $permalink, $post, $leavename ) {
// Get the category for the post
$category = get_the_category($post->ID);
if ( !empty($category) && $category[0]->cat_name == "b" ) {
$cat_name = strtolower($category[0]->cat_name);
$permalink = trailingslashit( home_url('/questions/' . $post->post_name .'/' ) );
}
return $permalink;
}
add_action( 'init', 'custom_rewrite_rules' );
function custom_rewrite_rules() {
add_rewrite_rule(
'b/([^/]+)(?:/([0-9]+))?/?$',
'index.php?category_name=b&name=$matches[1]&page=$matches[2]',
'top' // The rule position; either 'top' or 'bottom' (default).
);
}
Based on Customize “subsription option” string generated by WooCommerce Subscribe All The Things plugin answer code, this is my attempt:
function filter_wcsatt_single_product_subscription_option_description(
$option_description, $sub_price_html, $has_price_filter, $force_subscription, $product, $subscription_scheme ) {
// Class
$option_price_class = 'subscription-option';
// New description
$option_description = '<span>Subscription: </span>
<span>' . $sub_price_html . '</span>';
return $option_description;
}
add_filter( 'wcsatt_single_product_subscription_option_description', 'filter_wcsatt_single_product_subscription_option_description', 10, 6 );
Any advice?
This should suffice, basically anything you assign to the $none_string variable will be displayed
function filter_wcsatt_single_product_one_time_option_description( $none_string, $product ) {
$none_string = $product->get_price_html();
return $none_string;
}
add_filter( 'wcsatt_single_product_one_time_option_description', 'filter_wcsatt_single_product_one_time_option_description', 10, 2 );
The main price above "choose a purchase plan" is added by WooCommerce, and not by the WCSATT plugin. So to hide it, you can use:
function filter_woocommerce_available_variation( $data, $product, $variation ) {
$data['price_html'] = '';
return $data;
}
add_filter( 'woocommerce_available_variation', 'filter_woocommerce_available_variation', 10, 3 );
I want to make a visual editor for that I want when anyone clicks edit with particle studio then it's open a blank page with his my own layout
I am done with page_row_action code below
function ps_page_row_actions( $actions, $page_object ) {
$admin_post_url = admin_url( 'post.php?post=', 'http' );
$actions['particle_studio'] = '' . __('Edit with Particle Studio') . '';
return $actions;
}
add_filter('page_row_actions', 'ps_page_row_actions', 10, 2);
How can i go post.php?post=3&action=pspage with my own layout
function handle_post_action_ps( $post_id ) {
wp_die( 'Hello World '.$post_id );//wp_die is just for test
}
add_action( 'post_action_ps', 'handle_post_action_ps', 10, 1 );
The (post_action_ps) is the hook for your custom action "ps".
The function will handle the action.
You can set the name of the action to almost everything.
But in the URL must you must have same name for the action. Ex:
post.php?post=${post_id}&action=my-awesome-edit
add_action( 'post_action_my-awsome-edit', 'handle_my_awesome_edit', 10, 1 );
Ref links
post_action_{$action}: http://hookr.io/actions/post_action_action/
add_action: https://developer.wordpress.org/reference/functions/add_action/
I hope this helps! :D
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' );