How to map an arbitrary URL to a function in a Wordpress plugin - wordpress

I'm trying to create a Wordpress plugin that redirects visitors of example.com/redirect/XXX to a different page based on the value of XXX. I think I know how to do the redirect logic, but I don't know how to make sure that my Wordpress plugin function will be called when a visitor goes to example.com/redirect. Right now I just get a 404. There are other solutions that involved changing the .htaccess file, but I want this to function as a standalone plugin. Thanks!

When I need this kind of things i just create a common page with template as the "plugin", a page with all the functions that I need.
For example, if i need a shopping cart, I just create a cart.php as:
<?php
/*
Template Name: Cart
*/
// functions here
?>
And I go to my wp-admin and create a page with Cart as its template.

Depending on what exactly you want to do which is quite vague ( when you say page you actually mean page ? post ? cpt ? and when you say plugin functions - what are they ? and do you use permalinks ?)
.. but under some conditions you could use wp conditionals .
example ( from codex )
is_singular( 'foo' )
// Returns true if the post_type is "foo". execute plugin hook
is_singular( array( 'foo', 'bar', 'baz' ) )
// Returns true if the post_type is "foo", "bar", or "baz".
// See also the Custom Post Types book.
or if you aim at filtering you can always hook to pre_get_post with is_main_query() or any other conditional //

Related

Change the default global edit link that lists my CustomPostType (edit.php?post_type=CPT), to use a plugin page link instead

I'm working on a plugin (with a https://wppb.me/ plugin boilerplate base, if relevant).
I defined my custom post type, with the editor options I want, that I can edit with the default /wp-admin/post.php?post={id}&action=edit
The listing of those CPT is done on my plugin pages only (no specific menu for the CPT) and I want to keep it that way.
Problem:
I'd like to change the base edit link of this post type when no ID is specified
( eg : /wp-admin/edit.php?post_type=CPT ) to be the URL of my plugin
( eg : /wp-admin/admin.php?page=myplugin )
This, mostly because in Gutenberg Editor Fullscreen, the top left wordpress logo links to the /wp-admin/edit.php?post_type=CPT that I don't wont to use nor show. I'd like this link to be a page of my plugin (here, it would be the homepage of my plugin)
I tried with a filter on get_edit_post_link when there is no ID provided, but it doesn't seem to be the correct way to fix my problem.
Any tips or help to get me in the right direction in my research are welcome !
I found one of the possible solutions. After entering the site, you must redirect the user.
function wp_custom_update_meta_cache_filter() {
global $pagenow, $typenow;
if ( $pagenow == 'edit.php' && $typenow == 'CPT' ) {
wp_safe_redirect();
die();
}
}
add_action( 'admin_init', 'wp_custom_update_meta_cache_filter' );
Based in (233 line):
https://github.com/dipolukarov/wordpress/blob/master/wp-content/plugins/woocommerce/admin/woocommerce-admin-init.php

Show custom post archive when custom post not specified

I have a custom post type called produce set up in WordPress and a custom taxonomy called produce_category.
The URL format for posts in this custom post type is in this format http://mywebsite/produce/%produce_category%/%postname%/. Of course %produce_category% and %postname% get substituted accordingly, a working example url would be http://mywebsite/produce/fruits-and-vegetables/avocado.
What I would like to do is show the produce_category custom taxonomy archive if a user visits http://mywebsite/produce/%produce_category%, without specifying post name at the end, e.g http://mywebsite/produce/fruits-and-vegetables to show all produce in fruits-and-vegetables produce_category.
Besides that when a user visits http://mywebsite/produce/ I would like to show all the produce archive. EDIT: I have this working now.
I know how to create the archive pages totally fine and have no problem with that. I am stuck at creating permalinks. When I visit http://mywebsite/produce/%produce_category% I get a 404 error.
I'm looking for advise on the best way to implement this. Currently I am using Custom Post Type Permalinks and CPTUI.
The CPTUI custom taxonomy settings interface does not allow me to have a blank in the custom rewrite slug. It defaults to the custom taxonomy slug, produce_category, when I don't fill in anything.
This gives the front-side produce_category taxonomy archive url as http://mywebsite/produce/produce_category/%produce_category%/ e.g. http://mywebsite/produce/produce_category/fish-and-seafood/ when what I want for the archive is http://mywebsite/produce/fish-and-seafood/.
Please help with suggestion on the best way I can achieve the custom taxonomy url.
Thank you all.
Try this code. It will help you to achieve your url structure... Make sure you update permalinks after saving it to functions.php
function custom_produce_category_link( $link, $term, $taxonomy )
{
if ( $taxonomy !== 'produce_category' )
return $link;
return str_replace( 'produce_category/', '', $link );
}
add_filter( 'term_link', 'custom_produce_category_link', 10, 3 );

Customize search results for Custom Post Types

I am writing a Wordpress plug-in that creates several Custom Post Types (CPT). For they have their own custom fields, that need to be displayed in the search results, I need to customize the search results output.
Do I need to write my own theme for that or is there a hook (or other way) to solve this in my plug-in code?
You could hook into get_the_content and get_the_excerpt filters and test with is_search() to see if you should alter the returned value or not.
Not tested, but this is the idea:
add_filter( 'get_the_excerpt', 'my_search_excerpt' );
add_filter( 'get_the_content', 'my_search_excerpt' );
function my_search_excerpt( $content ) {
if ( is_search() ) {
$content = 'This is a search excerpt for ' . get_the_title();
// maybe add a read more link
// also, you can use global $post to access the current search result
}
return $content;
}
I see four possibilities:
Create a new (child) theme
Override the search template using filters (template_include)
Use client-side code to modify the appearance (CSS / JavaScript, poor workaround)
Hook the_content or the_excerpt
The easiest way might be to copy the search.php file of your installed theme and modify it to fulfill your needs. You can then hook it in using the first or second way. The first requires you to create a child theme, the second to create a plugin. The latter might be more complex so I would suggest to create a theme (take a look at template files of child themes for an explanation).

is it possible to make a custom page in buddypress

is it possible to make a custom page for buddypress with its url like this: http://domain.com/custom_page ? I found some answers by searching google but it does not create a custom page. i have a code here that i found in one of the blogs.
define('BP_FUN_SLUG','fun');
function bp_show_fun_page() {
global $bp, $current_blog;
if ( $bp->current_component == BP_FUN_SLUG && $bp->current_action == '' ) {
// The first variable here must match the name of your template file below
bp_core_load_template( 'fun', true );
}
}
add_action( 'wp', 'bp_show_fun_page', 2 );
but this code does not work... Is anyone there knows how to do this? thanks
Yes, it is possible to create a new page in Buddypress.
In Buddypress you have to create a new plugin or create a function in the theme functions file.
For creating first you have to add a new page link in navigation menu using bp_core_new_nav_item() function (You have created sub menu for that use bp_core_new_subnav_item() function).
Above two functions pass the screen function name as a parameter this name use when you click the custom page link call to this screen function. Create new function in your functions.php file same as screen function name. In this function call to your custom template file using bp_core_load_template() function.
Then finish, add more logic to create a new function and call it in the template file.
Another approach is to add a plugin that allows php in posts. For example http://wordpress.org/extend/plugins/allow-php-in-posts-and-pages/
Then create a page and add this to it:
[php] locate_template( array( 'some-template-folder/something.php' ), true ); [/php]
In case someone is wondering how to integrate custom pages into user profile (so that it looks like activity stream, groups etc.).
One thing i did recently was define a plugin (functions.php would work as well), register a custom slug with bp_core_new_nav_item or bp_core_nav_subnav_item and loaded member/single/plugins.php template in the handlers for those slugs. There are a bunch of actions on that page that you can hook into like bp_template_title and bp_template_content.
This can give you a whole lot of control.
Here's a link to the source of plugins.php: http://phpxref.ftwr.co.uk/buddypress/nav.html?readme.txt.source.html

WordPress Thesis theme - hook below post

i need a little help with thesis theme, i made a simple hook for single post with custom content and i want the hook to be right after the post but after the post there's a plugins like facebook etc... and my hook is below that. How to make my hook to be right after post without disabling this plugins ?
*i already tried every posible combinations thesis_hook_after_post, thesis_hook_after_post_box etc and it dont work
Thanks in advance !
Change priority value in add_action
add_action( $tag, $function_to_add, $priority, $accepted_args );
(int) (optional) Used to specify the order in which the functions
associated with a particular action are executed. Lower numbers
correspond with earlier execution, and functions with the same
priority are executed in the order in which they were added to the
action. Default: 10
Edit:-
function post_article111() {
if (is_single( )) {
echo "content goes here..";
}
}
add_action('thesis_hook_after_post_box', 'post_article111','5');

Resources