I am extending the newsletter area so that I can send out an email with a list of products but I can't find out how to get the seo url for each product?
When I do this I actually end up with the admin URL.
$this->url->link('product/product', 'product_id=' . $row['product_id'])
This same code in the store front works fine and returns the seo url if it exists.
The only way I can get it to work is if I manually build the URL, obviously no seo url.
HTTP_CATALOG .'index.php?route=product/product&product_id='.$row['product_id']
Looking into this further I see that the admin area is missing the following code but I can't figure out how this actually works and ties in with $this->url->link so that I can modify it to work for me.
$controller->addPreAction(new Action('common/seo_url'));
UPDATE - in the end the easiest solution was to add my own method like this:
public function getUrl($route, $key, $value){
$url = "index.php?route={$route}&{$key}={$value}";
if($this->config->get('config_seo_url')){
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = '" . $this->db->escape($key . '=' . (int)$value) . "'");
if($query->row['keyword']){
$url = $query->row['keyword'];
}
}
return $url;
}
Front-end links
The best way to make a front-end link from the Admin area without modifying core files is to create your own URL object:
$url = new Url(HTTP_CATALOG, $this->config->get('config_secure') ? HTTP_CATALOG : HTTPS_CATALOG);
To create URLs you then use $url instead of $this->url, so your example would be:
$url->link('product/product', 'product_id=' . $row['product_id']);
SEO URLs
Getting SEO URLs is a bit harder and unfortunately requires copying some code or a bit of bodging.
Copied Code
Basically, copy the entire rewrite($link) function from catalog/controller/common/seo_url.php and add it to the controller class that you're creating. Then add the following line after the new $url line mentioned above:
$url->addRewrite($this);
The current controller is then used as a rewriter and all URLs get rewritten. You might be able to use a separate class, but then there are dependencies to get the database access. This seemed the most direct way of doing it, even if it is ugly.
Your code should then be:
<?php
class ControllerMyController extends Controller {
public function index() {
...
$url = new Url(HTTP_CATALOG, $this->config->get('config_secure') ? HTTP_CATALOG : HTTPS_CATALOG);
if ($this->config->get('config_seo_url')) {
$url->addRewrite($this);
}
$url->link('product/product', 'product_id=' . $row['product_id']);
...
}
public function rewrite($link) {
... [stuff from seo_url.php] ...
}
}
and you'll get SEO'd URLs from the admin area.
Bodging
If you're happy with an arbitrary require statement, then you can alternatively do the following and use the existing SEO code (which means that it'll stay up-to-date if it changes, but will fail if the file moves).
To create SEO URLs this way your code needs to be:
$url = new Url(HTTP_CATALOG, $this->config->get('config_secure') ? HTTP_CATALOG : HTTPS_CATALOG);
if ($this->config->get('config_seo_url')) {
// Require the SEO file directly - path is relative to /admin/index.php
require_once('../catalog/controller/common/seo_url.php');
$rewriter = new ControllerCommonSeoUrl($this->registry);
$url->addRewrite($rewriter);
}
$url->link('product/product', 'product_id=' . $row['product_id']);
Administration part do not need the preAction for SEO, as there are no SEO links in administration.
The problem You are getting is because on frontend the constant HTTP[S]_SERVER points to http[s]://mydomain.com/ while in administration the same constant points to http[s]://mydomain.com/admin/.
What are You looking for is maybe an edit of url class (system/library/url.php) and adding a new method frontend_link by copying link method and replacing HTTP[S]_SERVER constant in the method body by HTTP[S]_CATALOG constant that is present only in administration...
Expanding a little on #IBBoard's very informative answer: in Opencart 3.0.2 and above, seo_url.php is no longer in the catalog/common folder. It was moved to catalog/startup.
Also, it is highly recommended to run the filename in the require_once statement through opencart's modification function, like:
require_once modification(DIR_CATALOG . 'controller/startup/seo_url.php');
Related
I'm completely new and not a programmer, I'm just experimenting with the functions.php file (in a child theme) to see if I can get a function to work.
My goal: I am using a multisite wordpress setup for a website on models. When admin creates a profile for each model, their name is used on SITE 1 for their unique url AND SITE 2 to say 'welcome model name'
SITE 1: public site for everyone to see
SITE 2: private models only site to view and update personal details
On SITE 1, I have a complete list of all the models on a page for the public to browse through. Each model has their own unique profile URL that contains their first and last name. E.g. http://www.domain.com/models/samantha-rebecca
On SITE 2, when the models log in to the site, I need the homepage to show 2 name uses. One is their display name using a shortcode so I can say 'Welcome Samantha Rebecca' (which I have got working using this code).
/* username in page content using [current_user] shortcode */
function custom_shortcode_func() {
ob_start();
$current_user = wp_get_current_user();
echo $current_user->display_name;
$output = ob_get_clean();
return $output;
}
add_shortcode('current_user', 'custom_shortcode_func');
The other is for a button on the same private page they can press to go straight to their 'public' profile page. This is what I need help with please.
In an ideal world, I would like to take their first and last name (all profiles are admin created only) and put a dash in between so I can use http://www.domain.com/models/[url_user] for which I did try and use the following:
/* username in url using [url_user] shortcode */
function custom_shortcode_func() {
ob_start();
$current_user = wp_get_current_user();
echo $current_user->user_firstname . '-' . $current_user->user_lastname;
$output = ob_get_clean();
return $output;
}
add_shortcode('url_user', 'custom_shortcode_func');
But I can't seem to get them to both work at the same time in the functions file without an error coming up such as this
Fatal error: Cannot redeclare custom_shortcode_func() (previously declared in /home/domain/public_html/wp-content/themes/child-theme/functions.php:47) in /home/domain/public_html/wp-content/themes/child-theme/functions.php on line 65
Please can someone help me get this to work as one efficient function. Thank you.
i got your issue is in both short code you have used same function name please change your second function name like below code.
/* username in url using [url_user] shortcode */
function custom_shortcode_func_url_username() {
ob_start();
$current_user = wp_get_current_user();
$output = $current_user->user_firstname . '-' . $current_user->user_lastname;
return $output;
}
add_shortcode('url_user', 'custom_shortcode_func_url_username');
change your second function name like above code your problem will solved. you are getting error because for both shortcode your function name is same. function name should be unique. same name multiple time is not allowed.
more about used define function name rules. More about used define functions
How to get parameters from below url
domain.com/admin/edit/12
I want to access this value (12) in edit function.
I searched around but didn't found any inbuilt solution in zend framework.
Even in other framework it works easily.
Like in codeIgnitor it works as segment and function parameter.
How I can see from code , you have Admin_Controller & some action edit ( by default routing) . For getting edit value , U need generate urls like domain.com/admin/edit/id/12/ (for example) . And than in action edit use next:
$id = $this->_request->getParam('id',0);
if ($id){
//get info for edit by ID
}
EDIT
IF you still want urls like domain.com/admin/edit/12, do next:
$uri = $this->_request->getRequestUri(); // or $this->getRequest()->getRequestUri()
$id = intval(end(explode('/',$uri)));
if($id){
// do something
}
I have a problem concerning url-rewriting in wordpress. I am currently working on a language plugin (nearly finished also) and as a last thing, I would like to see every url altered so that it contains the current language that has been selected by the user (or a default language if the user hasn't changed the language).
I don't have a problem altering the links, the problem lies with the rewriting done by the server. Below you can find how I change the links.
public function register_filters()
{
add_filter('page_link', array(get_class(),'alter_permalink'));
add_filter('post_link', array(get_class(),'alter_permalink'));
}
public function alter_permalink($permalink)
{
$permalink = str_replace(get_option('home'), '', $permalink);
$permalink = trim($permalink, '/');
//The next line is actually a method that is being called,
//but it will return a string like this.
$lang = 'EN';
return get_option('home') . '/' . $lang . '/' . $permalink;
//This returns a link that looks something like this:
//http://somedomain.com/EN/permalink-structure
}
So as you can see, I have no problems creating the links, the problem lies with the url-rewriting on the server itself.
I have tried this method: http://shibashake.com/wordpress-theme/wordpress-permalink-add
but I didn't get that one to work either. The problem is that I just don't seem to understand how these rewriting-rules work and that I can't seem to find a decent tutorial on the subject either.
Any help would be greatly appreciated.
the filter to listen to for handling url is request.
function request_handler($vars) {
//modified $vars here
return $vars;
}
add_filter('request', 'request_handler', 11);
i have written a plugin for customize url in wp. check out the source and see how i handle it.
http://wordpress.org/extend/plugins/auto-url/
I have a custom component I'm working on and I'm writing an import script which is running in the administration area. I have the following code:
$newUrl = ContentHelperRoute::getArticleRoute($import->article_id.':'.$import->alias, $import->catid);
$newUrl = JRoute::_($newUrl);
the first part works returning similiar to this:
index.php?option=com_content&view=article&id=45:joomla-sociable-and-sharethis-module&catid=18
the second part shows it like this:
/administrator/index.php?option=com_content&view=article&id=45:joomla-sociable-and-sharethis-module&catid=18
Both of the above urls are as you'd expect the component com_content to render these urls as if I wanted to use them within the administration area.
Any idea how to force JRoute to work as it would when used in the frontend?
NB: This is being used within a controller of my component, if it makes any difference and I'm including require_once (JPATH_SITE . '/components/com_content/helpers/route.php');
For those who find this on Google and struggle with using JRoute::_() and contentHelper::getArticleRoute().
$newUrl = ContentHelperRoute::getArticleRoute($import->article_id.':'.$import->alias, $import->catid);
// better will be check if SEF option is enable!
$router = new JRouterSite(array('mode'=>JROUTER_MODE_SEF));
$newUrl = $router->build($newUrl)->toString(array('path', 'query', 'fragment'));
// SEF URL !
$newUrl = str_replace('/administrator/', '', $newUrl);
//and now the tidying, as Joomlas JRoute makes a cockup of the urls.
$newUrl = str_replace('component/content/article/', '', $newUrl);
Here's a snippet that will work for Joomla 3.6
$routerOptions = [];
if (JFactory::getConfig()->get('sef')) {
$routerOptions['mode'] = JROUTER_MODE_SEF;
}
$siteRouter = JRouter::getInstance('site', $routerOptions);
$link = $siteRouter->build($yourRoute)->toString();
$link = preg_replace('#^/administrator#', '', $link);
A nicer solution would be to create a new router instance, so, the code will be something like this:
$app = JApplication::getInstance('site');
$router = &$app->getRouter();
$newUrl = ContentHelperRoute::getArticleRoute($import->article_id.':'.$import->alias, $import->catid);
$newUrl = $router->build($newUrl);
$parsed_url = $newUrl->toString();
$parsed_url = str_replace('/administrator', '', $parsed_url);
This way you will always obtain the right URL for the item, no matter if it is a joomla article, K2 article, etc...
** Notice that depending on the type of the item ( k2, joomla, etc) , $newUrl should be obtained with the consequent method.
I think that this one would be an easier solution:
$newUrl = JRoute::_(ContentHelperRoute::getArticleRoute($import->id.':'.$import->alias, $import->catid));
This will give you the same result as the other two previous answers but with less coding.
Hope this helps.
In Joomla 3.9 they extended the JRoute class (now called Route class) to include a link() static method which solves this problem.
use Joomla\CMS\Router\Route;
$newUrl = ContentHelperRoute::getArticleRoute($import->article_id.':'.$import->alias, $import->catid);
$newUrl = Route::link("site", $newUrl);
Route::link() works just the same as Route::_() except that you can must provide the additional first parameter to specify the client you want the URL built for. See https://api.joomla.org/cms-3/classes/Joomla.CMS.Router.Route.html#method_link.
A few people have touched on it, but no-one appears to have outright asked it. I'm trying to provide custom application of the link list in the sidebar of a template for a client. There seems to be very little customisation of the layout of the link list element. For instance, I'd like to show the title before the image and wrap the whole lot (title, image and description) in the anchor.
I've just started playing with the functions.php file, but can't seem to work this one out. Anyone have a sample function I could use for this?
Cheers,
T
If you mean the bookmark list?
You call into the filter with e.g.:
add_filter($this->_filter, array($this,'ReplaceAll'), 9); (in a class)
or
add_filter('some_filter', 'ReplaceAll', 9); (not in a class)
Where $this-filter is your filter e.g. 'bookmark_list' and 'ReplaceAll' is the function you will write. See: http://codex.wordpress.org/Plugin_API/Filter_Reference and check the chapter "blogroll filters" for most available filters.
you can then write your function 'ReplaceAll' like usual e.g.
function ReplaceAll($something_that_comes_in_from_the_filter)
{
// do stuff e.g. $something_that_comes_in_from_the_filter =
$something_that_comes_in_from_the_filter . ' hello world';
return $something_that_comes_in_from_the_filter;
}
In terms of functionality inside that function you can define e.g. a regular expression:
const HTML_REF_REGEX2 = '/<a(.*?)href=[\'"](.*?)[\'"](.*?)>(.*?)<\\/a>/i';
and then reshuffle the parts with the matches e.g.:
return '<a' . $arrUrlMatches[1] . 'href="' . $arrUrlMatches[2]
. '"' . $arrUrlMatches[3] .'>' . 'hello world'. $arrUrlMatches[4] . '</a>';
(See: http://php.net/manual/en/function.preg-match.php for how that works)
So in that way you can make it look any way you want.