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);
Related
I need to update values in all posts of a wordpress installation on a regular base. I was gooing to use shortcodes to insert the request into the wordpress post. Then use a custom functions.php that holds all the variables that need to be updated from time to time.
I got it working. Somehow but not the way I intended to use it. I'm a total beginner. Please consider this when answering my questions.
I want to have a function that reads what comes after honda_ and displays the correct value in wordpress without having to create a separate shortcode for each variable.
When entering [honda_link] wordpress should display the value from honda_link. When entering [honda_longlink] the value from honda_longlink variable should get displayed. I don't want to create a shortcode for each value.
I came up with this as a working solution...
// Honda
function honda() {
$honda_link = 'www.honda.com';
$honda_longlink = 'http://www.honda.com';
$honda_free = 'Free';
$honda_new = '23.688 $';
$honda_mileage = '00';
return $honda_link;
}
add_shortcode('neu', 'honda_link');
I tried some approaches by using an array but it ultimately failed all the time. I also tried it with if statements but wasn't able to get the right value displayed.
Someone willing to help a noob? I think I need to see a working example in order to understand it. The code snippets I have been looking at (that do something similiar but not the same I want to achieve) did confuse me more than they helped me.
I came up with this / Which works in a way but... This isn't very comfortable to use.
add_shortcode('HONDA','HONDA_TEST');
function HONDA_TEST($atts = array(), $content = null, $tag){
shortcode_atts(array(
'var1' => 'default var1',
'var2' => false,
'var3' => false,
'var4' => false
), $atts);
if ($atts['var2'])
return 'honda2';
else if ($atts['var3'])
return 'honda3';
else if ($atts['var4'])
return 'honda4';
else
return 'honda1';
}
So now when using:
[HONDA var1="novalue"][/HONDA]
[HONDA var2="novalue"][/HONDA]
[HONDA var3="novalue"][/HONDA]
[HONDA var4="novalue"][/HONDA]
it shows:
honda1
honda2
honda3
honda4
and so on.
Is there a better way to achieve the intended goal from post #1 ? Any way I could import the $variables from 1st post in bulk for example?
I don't have a working WP setup right now to test, but could you try this:
add_shortcode('HONDA','HONDA_TEST');
function HONDA_TEST($atts = array(), $content = null, $tag){
$atts = shortcode_atts(array(
'model' => 1,
), $atts);
$myHondas = [1 => 'honda1', 'honda2', 'honda3'];
return isset($myHondas[$atts['model']]) ? $myHondas[$atts['model']] : 'unknown model id';
}
And use it with [HONDA model="1"], [HONDA model="2"]
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');
I am a newbie in coding. Can you help me this code that I got from jetpack.
function allow_my_post_types($allowed_post_types) {
$allowed_post_types[] = ‘wpdmpro’;
return $allowed_post_types;
}
add_filter( ‘rest_api_allowed_post_types’, ‘allow_my_post_types’ );
that code above was working perfectly. I allowed 'wpdmpro'
I want to call multiple post types. I want to include 'dwqa-question' and 'topic'. Please help me if I got wrong with the code that I just created because its not working after added those two post types.
function allow_my_post_types($allowed_post_types) {
$allowed_post_types[] = ‘wpdmpro’,’topic’,’dwqa-question’;
return $allowed_post_types;
}
add_filter( ‘rest_api_allowed_post_types’, ‘allow_my_post_types’ );
thank you so much for those who can help. I am just learning by myself. Thanks.
Try to add the values one by one to the array like this:
$allowed_post_types[] = ‘wpdmpro’;
$allowed_post_types[] = ’topic’;
$allowed_post_types[] = ’dwqa-question’;
Or you may find some useful solution here:
PHP Arrays
Try this
function allow_my_post_types($allowed_post_types) {
array_push($allowed_post_types,'wpdmpro','topic','dwqa-question');
return $allowed_post_types;
}
add_filter( ‘rest_api_allowed_post_types’, ‘allow_my_post_types’ );
We are building a custom component , for seo improve, and we are working now with joomla "setMetaData" function Docs -> here
The normal sintaxis is:
$doc->setMetaData( 'tag-name', 'tag-content' );
But we need to put a variable in "tag-content" , we use this code, but doesn´t work:
$doc->setMetaData( 'DC.Title', echo($params->get('page_description')) );
And this doesn´t work as previus:
$doc->setMetaData( 'DC.Title', $title );
Also we now that "tag-content" only accepts Strings, any idea to solve this?
Thanks for the help and you time.
You don't need to echo it. It is function parameter.
Use this:
$doc->setMetaData( 'DC.Title', $doc->getDescription() );
First, did you get the $doc object using?
$doc = JFactory::getDocument();
You don't need to echo the get function for $params.The following should work assuming that the $params object is initialized and not empty.
You can also pass a default value to the get function for testing:
$doc->setMetaData( 'DC.Title', $params->get('page_description' , 'defaultValue') );
This will return 'defaultValue' if 'page_description' is empty.
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.