Change default user profile URL - BBpress Plugin - php

Bbpress Wordpress Plugin have default link user profile url. The link like this: www.example.com/forum/users/(username)
The main purpose in nutshell is: I want to change the url.
Actually, I found the solution but its not perfect. The code like this:
function user_profile_link(){
$url = 'http://localhost/example.com/profile/';
$author_id = bbp_get_reply_author_id();
$user_info = get_userdata($author_id);
echo ' '. $user_info->display_name.' ';
}
add_filter('bbp_get_user_profile_url', 'user_profile_link');
Yes, the code working well. But the outcome is, the user profile URL not replaced and there is double URL like this image below:
image1
I think the problem solved if I display: none it. The code like this:
<style>
.bbp-author-link{
display: none;
}
</style>
But there is one problem. The new URL that I make appeared beside the breadcrumbs like this image:
image2
I want to remove the link that appeared beside the breadcrumbs. Is there any solution? Any help is appreciated. Thank You

In a filter hook, you normally have to override the current value by returning it. Therefore try returning the new value by using the function you already created. It may remove the duplicate.
Also, use site_url() instead of $url variable because there will be issues when you use a hardcoded URL.
function user_profile_link(){
$author_id = bbp_get_reply_author_id();
$user_info = get_userdata($author_id);
return site_url()."/profile/".$user_info->user_login;
}
add_filter('bbp_get_user_profile_url', 'user_profile_link');

For this problem, I found the solution.
The code is like this:
function user_profile_link(){
$author_id = bbp_get_reply_author_id();
$user_info = get_userdata($author_id);
$url = site_url()."/profile/".$user_info->user_login;
return $url;
}
add_filter('bbp_get_user_profile_url', 'user_profile_link');

Related

How to automatically insert text in WordPress?

I'm wondering if there is a way to insert text just like short codes. For an example, whenever I typed %sitename, I want it to automatically get the predefined site name.
To clarify it further, I want it to be like this.
1. %sitename -> mysite
2. %siteurl -> mysite.com
Is there anyway I can do this? Any help is greatly appreciated. Thanks.
If you want to apply this to the content of a post you can try something like this:
function make_post_tags($content)
{
$what = array('%sitename', '%siteurl');
$with = array(
get_bloginfo('name'),
get_bloginfo('url'),
);
return str_repalce($what, $with, $content);
}
add_filter('the_content', 'make_post_tags', 100, 1);

Wordpress Plugin Using Permalinks

So i created a plugin that is basically a gallery that you choose options as you go.
First choose Brand
Then Color
Then Style
At each step i am passing the variables via $_GET
So once you have chosen your brand and continue the next page URL is cabinets/?brand=1
Then after you choose your color it is cabinets/?brand=1&color=2
i have written a rewrite for this that is supposed to make pretty urls but all it is doing is showing the home page.
add_filter('rewrite_rules_array','cabinets_rewrite_rules_array');
function cabinets_rewrite_rules_array($rules){
$cabinets_slug = 'cabinets';
$my_cab_rules[$cabinets_slug.'/?$'] = $cabinets_slug."/?brand=$matches[1]";
$my_cab_rules[$cabinets_slug.'/(.+?)/?$'] = $cabinets_slug."/?brand=$matches[1]&color=$matches[2]";
$my_cab_rules[$cabinets_slug.'/(.+?)/(.+?)/?$'] = $cabinets_slug."/?brand=$matches[1]&color=$matches[2]&style=$matches[3]";
return $my_cab_rules + $rules;
}
i have tried many things even as much as updating the htaccess file but i dont want to have to do that since this is a plugin.
Any Idea?
You could try adding your new variables to the list of query vars.
function prfx_add_query_vars($aVars) {
global $wp_query;
$aVars[] = "brand";
$aVars[] = "color";
$aVars[] = "style";
return $aVars;
}
add_filter('query_vars', 'prfx_add_query_vars');

PHP: How to remove duplicate arguments in a url?

I'm trying to write a product filter extension for opencart.
I assign size, color etc. options to the url like this:
index.php?route=product/category&path=59_63&size=57&color=black
The problem is when I click another color on the page the link goes like this:
index.php?route=product/category&path=59_63&size=57&color=black&color=brown
As you can see there are duplicated color arguments and it messes up the category listing.
How can I remove same arguments if there is?
The original opencart's link builder function:
public function link($route, $args = '', $connection = 'NONSSL') {
if ($connection == 'NONSSL') {
$url = $this->url;
} else {
$url = $this->ssl;
}
$url .= 'index.php?route=' . $route;
if ($args) {
$url .= str_replace('&', '&', '&' . ltrim($args, '&'));
}
return $this->rewrite($url);
}
There is not enough information to provide a correct answer, but I'll take a guess.
The problem seems to be with $args. It seems that you are taking $args from the URL and append to it your new color parameter.
If URL is index.php?route=product/category&path=59_63&size=57&color=black, then $args is path=59_63&size=57&color=black
You append to it color=brown and $args becomes path=59_63&size=57&color=black&color=brown.
If this is the case, you can do something like this:
parse_str($args,$url_params);
$url_params['color'] = 'brown'; //-- overwrites color=black with color=brown
$args = http_build_query($url_params);
Then pass $args to your link() function.
you don't have to remove duplicate parameters.
uou have not to add it.
use http_build_query() to create a query string
This is really nothing to do with opencart's link builder, it's done outside of that so as not to make any core changes. You need to set the value when you use $this->url->link in your controller code for your category. When you are getting all of the colors that you will use for filters, be sure to unset the color attribute passed into the second parameter of the link

Make SEO sensitive URL (avoid id) Zend framework

i have url like this :
http://quickstart.local/public/category1/product2
and in url (category1/product2) numbers are id , categorys and products fetched from database attention to the id
id is unique
i need to the sensitive url like zend framework url. for example :http://stackoverflow.com/questions/621380/seo-url-structure
how i can convert that url to the new url like this
is there any way?!!
You'll need to store a unique value in your database with a field name such as 'url' or something similar. Every time you generate a new product you will have to create this unique url and store it with the product information. A common way to do this is to take the name of the product and make it url friendly:
public function generateUrl($name)
{
$alias = str_replace(' ', '-', strtolower(trim($name)));
return preg_replace('/[^A-Za-z0-9-]/', '', $alias);
}
Calling this method:
$url = $this->generateUrl("My amazing product!");
echo $url;
will output:
my-amazing-product
You'll need to check that the output from this function does not already exist in the database as you will use this value to query on instead of the id.
If you apply this logic to the categories as well, you can have easily readable and descriptive urls like the one below. You may need to tweak your routing before this works correctly though.
http://quickstart.local/public/awesome-stuff/my-amazing-product
You could use ZF's Zend_Controller_Router_Route. For example, to make similar url to those used by SO, one could define a custom route in an application.ini as follows (assuming you have controller and action called questions and show respectively):
resources.router.routes.questions.route = '/questions/:id/:title'
resources.router.routes.questions.type = "Zend_Controller_Router_Route"
resources.router.routes.questions.defaults.module = default
resources.router.routes.questions.defaults.controller = questions
resources.router.routes.questions.defaults.action = show
resources.router.routes.questions.defaults.id =
resources.router.routes.questions.defaults.title =
resources.router.routes.questions.reqs.id = "\d+"
Having such a route, in your views you could generate an url as follows:
<?php echo $this->url(array('id'=>621380,'title' => 'seo url structure'),'questions');
// results in: /myapp/public/questions/621380/seo+url+structure
//OR if you really want to have dashes in your title:
<?php echo $this->url(array('id'=>621380,'title' => preg_replace('/\s+/','-','seo url structure'),'questions');
// results in: /myapp/public/questions/621380/seo-url-structure
Note that /myapp/public/ is in the url generated because I don't have virtual hosts setup on my localhost nor any modifications of .htaccess made. Also note that you don't need to have unique :title, because your real id is in :id variable.
As a side note, if you wanted to make it slightly more user friendly, it would be better to have your url as /question/621380/see-url-structure rather than /questions/621380/see-url-structure. This is because under this url you would have only one question, not many questions. This could be simply done by changing the route to the following resources.router.routes.questions.route = '/question/:id/:title'.
EDIT:
And what to do with categories and products that you have in your question? So, I would define a custom route, but this time using Zend_Controller_Router_Route_Regex:
resources.router.routes.questions.route = '/questions/(\d+)-(d+)/(\w*)'
resources.router.routes.questions.type = "Zend_Controller_Router_Route_Regex"
resources.router.routes.questions.defaults.module = default
resources.router.routes.questions.defaults.controller = questions
resources.router.routes.questions.defaults.action = show
resources.router.routes.questions.map.1 = category
resources.router.routes.questions.map.2 = product
resources.router.routes.questions.map.3 = title
resources.router.routes.questions.reverse = "questions/%d-%d/%s"
The url for this route would be then generated:
<?php echo $this->url(array('category' => 6213,'product' => 80,'title' => preg_replace('/\s+/', '-', 'seo url structure')),'questions' ); ?>
// results in: /myapp/public/questions/6213-80/seo-url-structure
Hope this will help or at least point you in the right direction.

Joomla : how to get the url of a specific Menu itemID?

Friends a newbie question.........I need help in getting the URL of a specific Menu itemID. The situation is like this:
I am running Joomla and asking for a user to input for a menu ID and choose a layout for that menu ID.
I want to do something else with this URL of the Menu itemID.
How can I get the URL of this Menu itemID provided by the user?
For Example if the user input is liek $this->get ('menulayoutid'>; and he inputs and ID of 54 then how do I get the URL for Menu ID 54.
Please note: I want to get this URL from within my PHP file and not in the browser so that I can use the value of that URL for some other purpose.
Kindly help.
$itemid = JRequest::getVar('Itemid');
$application = JFactory::getApplication();
$menu = $application->getMenu();
$item = $menu->getItem($itemid);
$link = new JURI($item->link);
$link->setVar('ItemId', $itemid);
Source: http://forum.joomla.org/viewtopic.php?p=1836005
However, we get the Itemid from anywhere (user input, from our own developed module using the "menu item" field type in the xml file as described in the Joomla Docs - Standard form field types)
// get the menuItemId from wherever...
// as described above or as in other posts here and do whatever with that!
$menuItemId = 'fromWherever'; // as an example "107";
// build the link to the menuItemId is just easy and simple
$url = JRoute::_('index.php?Itemid=' . $menuItemId);
i think if we need only a link to a specific menu id, this is the best solution, because we have absolutely less requests and a clean code
this works also in Joomla 3.0, 3.1
I just want to add that if you need to target a specific menu you pass the menu name as an argument to getMenu().
$itemid = JRequest::getVar('Itemid');
$application = JFactory::getApplication();
$menu = $application->getMenu( 'menu-name' );
$item = $menu->getItem($itemid);
$link = new JURI($item->link);
$link->setVar('ItemId', $itemid);
I'm not sure if Joomla changed the way this works since 2.5 or even 1.7 but I spent the worse half of 2 hours looking for this.
Hopefully it helps someone.
$menuID = $params->get('menuItem'); // from module field menu ex. '105'
$js = new JSite;
$menu = $js->getMenu();
$link = $menu->getItem($menuID)->route;
//Returns URL Friendly Link -> menu/article
//Then format it ->
$link = 'http://www.yoursite.com/index.php/'.$link;
echo 'Borrowed Menu Link Path";
When you need to get your active menu item ID in Joomla to display some specific content for only that menu item or just to show the ID of the menu item, insert the following code where you wish to display the active menu item ID:
<?php
$currentMenuId = JSite::getMenu()->getActive()->id;
echo $currentMenuId;
?>

Categories