Site URL and Admin URL shortcode issue in wordpress - wordpress

I am trying have site url and admin url as shortcode in wordpres.
Code I am using is written below.
//Website URL shortcode [site_url]
add_action( 'init', function() {
add_shortcode( 'site_url', function( $atts = null, $content = null ) {
return site_url();
} );
} );
//Website admin URL shortcode [admin_url]
add_action( 'init', function() {
add_shortcode( 'admin_url', function( $atts = null, $content = null ) {
return admin_url();
} );
} );
Now the issue is, when I use [site_url] or [admin_url] it show url https//mysitename.com. Mean the link is without ":" in URL
I tried to use code below to replace https// with https:// but that one also not working...
//Replace text
function replace_text($my_text_replace) {
$my_text_replace = str_replace('https//', 'https://', $my_text_replace);
$my_text_replace = str_replace('https//https//', 'https://', $my_text_replace);
return $my_text_replace;
}
add_filter('the_content', 'replace_text');
Any suggestion please..
Thanks

I think you should check inside your database, in wp_options table the values of this 2 key: "home" and "siteurl". Since those functions actually read that value, go in the database and make sure 100% you didn't miss the double dots there

Related

Wordpress Custom Plugin: Query String to Dynamic Url

What I currently have developed:
http://wordpress.test/hashtag/result/?hashtag=twerk
What I want to achieve:
http://wordpress.test/hashtag/twerk
I have made a custom plugin which currently reads the 'hashtag' (query string) and displays it through my custom shortcode.
/result/ is a static page with the custom shortcode [woink_hashtag_result]. This currently works as intended.
Here is what I've currently produced.
function woinkResult()
{
$return = '<p>' . get_query_var ('hashtag' ) . '</p>';
// Output needs to be return
return $return;
}
add_shortcode('woink_hashtag_result', 'woinkResult');
function pce_register_query_vars ( $vars ) {
$vars[] = 'hashtag';
return $vars;
}
add_filter ( 'query_vars', 'pce_register_query_vars' );
How can I make the url prettier like: http://wordpress.test/hashtag/twerk ?
Thanks if you have made it this far.

wordpress replace text before showing it to the browser

My website is built using Elementor using GeneratePress as default theme and I want to replace output final text with some values from the database. This plugin
Real-Time Find and Replace https://wordpress.org/plugins/real-time-find-and-replace/ is doing the replace functionality perfectly and it uses action:
//Handles find and replace for public pages
add_action( 'template_redirect', 'far_template_redirect' );
to replace text using code:
function far_ob_call( $buffer ) { // $buffer contains entire page
$far_settings = get_option( 'far_plugin_settings' );
if ( is_array( $far_settings['farfind'] ) ) {
foreach ( $far_settings['farfind'] as $key => $find ) {
if( isset( $far_settings['farregex'][$key] ) ) {
$buffer = preg_replace( $find, $far_settings['farreplace'][$key], $buffer );
} else {
$buffer = str_replace( $find, $far_settings['farreplace'][$key], $buffer );
}
}
}
return $buffer;
}
I can not use the plugin directly because it repalce text with text and I need to fetch data from database, so I have to make my own plugin. But, I found this blog: https://markjaquith.wordpress.com/2014/02/19/template_redirect-is-not-for-loading-templates/ that tells me to use a filter:
add_filter( 'template_include', 'my_callback' );
instead of the above action in plugin:
add_action( 'template_redirect', 'far_template_redirect' );
But the plugin is using the action.
What to use?
The plugin description says: "Set up find and replace rules that are executed AFTER a page is generated by WordPress, but BEFORE it is sent to a user's browser." this is what I exactly want. That is to replace text after page is generated and just before the page is sent to browser, but the plugin uses action for this, and other blogs suggest to use filter?

Removing the my account-dashboard and redirect to another endpoint like downloads

I don't want the customers to get linked to the myaccount-dashboard with the annoying Hello xy, from here you can blabla, when they click on "My Account".
Is there an endpoint for the my account dashboard? I didn't find it on the backend in woocommerce>Settings>Accounts...
What's working is: I set up a custom link under menu/navigation... called it "My Account" and set the link to /myaccount/downloads for example.
So, when customers are logged in and they click on My Account, they get redirected to Downloads.
I'm wondering, is there another way to get rid of the dashboard?
Or a redirect solution? Thanks.
Remove it using below function. change downloads to your requirement.
function custom_my_account_menu_items( $items ) {
unset($items['downloads']);
return $items;
}
add_filter( 'woocommerce_account_menu_items', 'custom_my_account_menu_items' );
Looking more closely it seems like there are a handful of ways to do this. Take a look at the my-account.php template. You could override this, but I found that if you remove the woocommerce_account_content hook, you get a deprecation notice and WooCommerce thinks you have an outdated template and adds the content anyway.
Looking at the template you will see two hooks. The side menu navigation is added to the woocommerce_account_navigation and the content is added to woocommerce_account_content function. You can remove the defaults from their hooks and add back just the downloads content.
function so_41983566_remove_account_dadshboard(){
remove_action( 'woocommerce_account_navigation', 'woocommerce_account_navigation' );
remove_action( 'woocommerce_account_content', 'woocommerce_account_content' );
add_action( 'woocommerce_account_content', 'so_41983566_download_content' );
}
add_action( 'woocommerce_account_navigation', 'so_41983566_remove_account_dadshboard', 1 );
function so_41983566_download_content(){
do_action( 'woocommerce_account_downloads_endpoint' );
}
Or woocommerce_account_content() and woocommerce_account_navigation() are both pluggable functions and you can just define new versions in your theme/plugin.
This link explains everything:
https://github.com/woocommerce/woocommerce/wiki/Customising-account-page-tabs
First you need to create an endpoint:
function my_custom_endpoints() {
add_rewrite_endpoint( 'my-custom-endpoint', EP_ROOT | EP_PAGES );
}
add_action( 'init', 'my_custom_endpoints' );
function my_custom_query_vars( $vars ) {
$vars[] = 'my-custom-endpoint';
return $vars;
}
add_filter( 'query_vars', 'my_custom_query_vars', 0 );
Then create the menu item:
function my_custom_my_account_menu_items( $items ) {
$logout = $items['customer-logout'];
unset( $items['customer-logout'] );
$items['my-custom-endpoint'] = __( 'My Custom Endpoint', 'woocommerce' );
$items['customer-logout'] = $logout;
return $items;
}
add_filter( 'woocommerce_account_menu_items', 'my_custom_my_account_menu_items' );
Add content to the endpoint
function my_custom_endpoint_content() {
echo '<p>Hello World!</p>';
}
add_action( 'woocommerce_account_my-custom-endpoint_endpoint', 'my_custom_endpoint_content' );

how to change search permalink in wordpress?

I have custom search page, that have permalink http://mywebsite.com/custom-search/
What should I do to pass the search keyword as a parameter, like this: http://mywebsite.com/custom-search/keyword
I get error 404 page. Or may be there a way to change standard permalink /search/ to /custom-search/ ?
You should use rewrite endpoints
A sample code :
/*!
* URL rewrite
*/
function my_custom_rewrite_rules() {
$page_id = 123;
$page_path = get_page_uri( $page_id );
add_rewrite_endpoint( 'keyword', EP_PAGES );
add_rewrite_rule('^'. $page_path .'/(.*)/?', 'index.php?page_id=' . $page_id . '&keyword=$matches[1]', 'top');
}
add_action('init', 'my_custom_rewrite_rules');
and then add it as a query_var
function my_custom_query_vars($vars) {
if( isset( $_GET['keyword'] ) && !empty( $_GET['keyword'] ) ) {
$vars[] = 'keyword';
}
return $vars;
}
add_filter( 'query_vars', 'my_custom_query_vars', 10, 1 );
you will be able to retrieve the value of the passed keyword via get_query_var("keyword")
hope it helps
Note : You must update your permalinks structure or use flush_rewrite_rules(); after adding these codes
just changed search base with function
function vital_custom_search_base() {
$GLOBALS['wp_rewrite']->search_base = 'custom-search';
}
add_action( 'init', 'vital_custom_search_base' );
function only works after resave in settings > permalinks

Add custom frontend page without menu item - wordpress

Im trying to do something like this.
Add "custom page" without page
I know about adding a wordpress page from admin panel, Pages->Add New, and then link this page to PHP file using the slug. I've already done that. I just want to make this page work without adding it from admin panel, in case if page gets deleted from admin panel it won't work even if exists in the directory.
Please let me know if my question isn't clear enough. Any help is highly appreciated.
Thanks!
Update:
Thanks to #Mike i was able to solve the problem by modifying his code. I just had to add add_rewrite_rule() and its working good now. Don't forget to flush permalinks.
function add_application_endpoint() {
add_rewrite_endpoint( 'view', EP_PERMALINK );
}
add_action( 'init', 'add_application_endpoint' );
function add_endpoint_queryvar( $query_vars ) {
$query_vars[] = 'view';
$query_vars[] = 'ptag';
$query_vars[] = 'product_cat';
return $query_vars;
}
add_filter( 'query_vars', 'add_endpoint_queryvar' );
add_rewrite_rule( '^view/([^/]+)/([^/]+)/?$', 'index.php?pagename=custom-product-tags&ptag=$matches[1]&product_cat=$matches[2]', 'top' );
/**
* Setting up job app template redirect for custom end point rewrite
*/
function job_application_template_redirect() {
global $wp_query;
if ( $wp_query->query_vars['name'] != 'custom-product-tags' ) {
return;
}
include dirname( __FILE__ ) . '/page-custom-product-tags.php';
exit;
}
add_action( 'template_redirect', 'job_application_template_redirect' );
You can do it by creating a custom endpoint and setting up a template redirect in your functions.php file.. Here is an example for a job application page. With this code added to my functions.php file, if I visit '/apply' on my site, the page-job_application.php template is rendered.
Hope this works for your needs.
/**
* Rewrite custom endpoint for job post applications
*/
function add_application_endpoint() {
add_rewrite_endpoint('apply', EP_PERMALINK);
}
add_action( 'init', 'add_application_endpoint');
/**
* Register our custom endpoint as a query var
*/
function add_endpoint_queryvar( $query_vars ) {
$query_vars[] = 'apply';
return $query_vars;
}
add_filter( 'query_vars', 'add_endpoint_queryvar' );
/**
* Setting up job app template redirect for custom end point rewrite
*/
function job_application_template_redirect() {
global $wp_query;
if ( ! isset( $wp_query->query_vars['apply'] ) || ! is_singular() )
return;
include dirname( __FILE__ ) . '/page-job_application.php';
exit;
}
add_action( 'template_redirect', 'job_application_template_redirect' );

Resources