I'm new in wordpress plugins development , I'd like to create a plugin do some special edits on posts after created. I want to add a link (anchor) under the post after created (like in the attached image) to redirect to the new page that I will create ..
Can I do something like this?
You can do it by creating a hook for post_row_actions filter
add_filter( 'post_row_actions', 'wpse8170_row_actions', 10, 2 );
function wpse8170_row_actions( $actions, WP_Post $post ) {
if ( $post->post_type != 'my-custom-post-type' ) {
return $actions;
}
$actions['wpse8170-pdf'] = 'http://your/url/here';
return $actions;
}
Read this
http://pippinsplugins.com/add-custom-links-to-user-row-actions/
or read this one
http://www.codeforest.net/add-custom-links-to-row-actions-in-wordpress
Related
I created a Custom Post Type, and it has some categories in it.
I created forms with User Frontend Pro for CPT categories.
I’m looking for a solution for assigning the custom templates to that records to show them on the website.
For example;
Assume that I have a CPT named Project.
The project post type has two categories; “Business” and “Ideas”
Users can post their entries with forms to these categories and their posts listed under the account dashboard.
The issue is that;
Business categories should be shown with the category-business.php custom template, and in a similar way, the Ideas category should be shown with category-ideas.php.
How can I add custom template to CPT categories?
P.S. I tried the template hierarchy solution that is in the WP documents, but it's not working. Because there is a custom view function for content. I looking for a code snippet that adds the template page attributes to custom posts automatically, if it is possible.
Assuming that you are just using the default WordPress category you can the pre_get_posts Hook to include the project as part of the category page if they are not showing by default
function include_project_to_cat_pages( $query ) {
if ( $query->is_category() && $query->is_main_query() ) {
$query->set( 'post_type', array( 'post', 'project' ) );
}
}
add_action( 'pre_get_posts', 'include_project_to_cat_pages' );
I solved that issue with a filter.
add_filter( 'single_template', function( $template ) {
global $post;
$cat = get_the_category()[0];
if ( $post->post_type === 'project' && $cat->slug === 'business') {
$locate_template = locate_template( "custom-business-template.php" );
if ( ! empty( $locate_template ) ) {
$template = $locate_template;
}
}
return $template;
} );
Thank you to all contributors and the community.
I'm trying to add a custom CSS class for the search page that uses WooCommerce Product Search.
Search pages path is like:
domain.com/shop/ixwpss=3&title=0&excerpt=0&content=0&categories=0&attributes=0&tags=1&sku=0&v=a3420e5a4c03
When I search for the number 3 that represents the tag for products.
I managed to add the class for the whole shop, with the following function but I want to add only for the search page. Can someone help me?
add_filter( 'body_class', 'add_body_classes' );
function add_body_classes( $classes ) {
global $post;
if ( is_shop() )
$classes[] = 'search';
return $classes;
}
Please update the code like below
add_filter( 'body_class', function( $classes ) {
if ( is_shop() ) {return array_merge( $classes, array( 'class-name' ) );}
}
Assuming that the search page is using the archive-product.php
If you are using a WooCommerce compatible theme then you have to go in your themes folder -> woocommerce and find the archive-product.php. If the override file does not exists then go to wp-content/plugins/woocommerce/templates/archive-product.php
And a if statement to check if is search or not like below:
if ( is_search() ) {
<body class="your-class">
} else {
<body class="regular-class">
// the content that is already in that file (archive-product.php)
}
*if the part (body class in your case) is not there you have to locate it and do the same (probably it's in your header.php)
I've added some code to my WP site that counts how many times a post has been viewed. I then added a column in the admin to show how many views the post has received. This is for standard WP blog posts:
// SHOW POST VIEWS COLUMN IN ADMIN...
add_filter('manage_posts_columns', 'posts_column_views');
add_action('manage_posts_custom_column',
'posts_custom_column_views',5,2);
function posts_column_views($defaults){
$defaults['post_views'] = __('Views');
return $defaults;
}
function posts_custom_column_views($column_name, $id){
if($column_name === 'post_views'){
echo my_get_post_views(get_the_ID());
}
}
This works fine, but the problem is that now this new column shows in all the custom post types as well. I wrote some code to unset it from the custom post types (see below) which works fine.
// BUT DON'T SHOW IT ON CUSTOM POST TYPES
function jxb_manage_columns( $columns ) {
unset($columns['post_views']);
return $columns;
}
function jxb_column_init() {
add_filter( 'manage_jxb-component_posts_columns' , 'jxb_manage_columns' );
add_filter( 'manage_faqs_posts_columns' , 'jxb_manage_columns' );
add_filter( 'manage_edr-component_posts_columns' , 'jxb_manage_columns' );
add_filter( 'manage_new-stories_posts_columns' , 'jxb_manage_columns' );
add_filter( 'manage_loc_posts_columns' , 'jxb_manage_columns' );
add_filter( 'manage_upcoming-event_posts_columns' , 'jxb_manage_columns' );
add_filter( 'manage_product-return_posts_columns' , 'jxb_manage_columns' );
add_filter( 'manage_splash-page_posts_columns' , 'jxb_manage_columns' );
}
add_action( 'admin_init' , 'jxb_column_init' );
Just wondering if there is a more efficient way to do this rather than to unset the column from each custom post type individually. Thanks!
By the WordPress codex documentation you can set the post type name on the filter manage_{$post_type}_posts_columns, see CODEX
So you can set the post (which is actually a post type) on {$post_type} then you will limit your filter to the post post types, so:
add_filter('manage_post_posts_columns', 'posts_column_views');
add_action('manage_post_posts_custom_column', 'posts_custom_column_views',5,2);
I'm using a WordPress plugin https://github.com/lesterchan/wp-postratings.
It also showing ratings on admin, when i visit http://domain.com/wp-admin/edit.php.
How do i remove those ratings from admin side.
You can use the below function in your functions.php file with the manage_posts_columns filter. I'm assuming your custom post type id 'tools' and the column is referenced by 'ratings'. If they are different you can just change that in the code.
add_filter( 'manage_posts_columns', 'custom_post_columns', 10, 2 );
function custom_post_columns( $columns, $post_type ) {
switch ( $post_type ) {
//assuming the id for the custom post type is 'tools'
case 'tools':
unset(
//I'm using 'ratings' but you'll have to check and see what the name for the column really is.
$columns['ratings']
);
break;
}
return $columns;
}
How can i disable the comments form for a specific category in Wordpress.
And i mean with that every post the user will publish it in this category will not has a comment form content.
Rather than doing this in functions.php, you could create two template files. The first template file is your standard theme template, the second would be identical, except it wouldn't contain the comment code section.
Simply name the template file that doesn't have the comment code block category_{id}.php and upload to your theme folder. The ID is the ID of the category you want to disable comments on.
More information on category specific templates here https://developer.wordpress.org/themes/basics/template-hierarchy/#category
More information about the comment template here https://codex.wordpress.org/Function_Reference/comments_template
If you still want to do this via functions.php, see this blog post http://spicemailer.com/wordpress/disable-hide-comments-posts-specific-categories/ which uses the following code snippet
add_action( 'the_post', 'st_check_for_closed' );
function st_check_for_closed()
{
global $post;
$my_post_cat = wp_get_post_categories($post->ID);
$disabled_cat = array( "1", "3"); // this is he array of disabled categories. Feel free to edit this line as per your needs.
$my_result = array_intersect($my_post_cat,$disabled_cat);
if (empty ( $my_result ) )
{
return;
}
else {
add_filter( 'comments_open', 'st_close_comments_on_category', 10, 2 );
add_action('wp_enqueue_scripts', 'st_deregister_reply_js');
}
}
function st_deregister_reply_js()
{
wp_deregister_script( 'comment-reply' );
}
function st_close_comments_on_category ($open, $post_id)
{
$open = false;
}