I want to construct a very basic shortcode solution in a simple non-WordPress website I am maintaining, to allow users to enter content that will be pulled from a database and rendered to screen, allowing them to create a href buttons.
I've seen various classes for this but want to do this in just a line or two of code if possible.
Only they will enter via a shortcode with this format:
[BUTTON link="www.test.com" label="Click here"]
So far I have this which doesn't extract the attributes properly:
$copy=preg_replace('/\[(BUTTON )(.*?)\]/', '$1', $copy);
Can anyone advise?
I assume that you don't actually want to capture the BUTTON, but the link and the label.
Code: (Demo)
echo preg_replace(
'/\[button link="([^"]+)" label="([^"]+)"]/i',
'$2',
$text
);
maybe check how WordPress implemented it?
https://github.com/WordPress/wordpress-develop/blob/6.0.2/src/wp-includes/shortcodes.php
and if the above link doesn't work, check up WordPress core , how they implemented the short code functionality on GitHub.
Related
Wordpress | Advanced Custom Fields plugin
I created a new field in the editor where my colleagues can fill in a URL relevant to that specific page. This URL will be used in the button we load on every single job listing page. This button is created within the functions.php file:
function my_editor_content( $content ) {
$content = "<h3>Title</h3><p>text</P>
<a href='[acf field='afas_url']' target='_blank' rel='noopener'><button>Solliciteer Nu</button></a>";
The above is cut off at the 'afas_url' part. So, I think it has something to do with the quotations marks and probably not even hard to fix, but as I'm just starting to figure things out I couldn't find the answer myself yet.
I hope it's a clear enough explanation :)
(And maybe it's not best practice to use functions.php but it was already like this)
I'm running a simple wordpress site where there is a possibility for users to log in. I'm now at a point where I want to display different HTML content based on the currently logged in user.
I know that this can be solved by using PHP and I even have a basic knowledge in using PHP but I just don't know where and how to start.
Thanks for every input and best regards
It is quite simple to do this, once you get to know your way around PHP. You can use this by using shortcodes (which, in HTML, you can call with do_shortcode term) so you will be able to use it with shortcodes and in HTML if needed.
You will first have to register a shortcode first. As you said, you only want a different HTML content for logged in users. To create a shortcode open your functions.php file and copy/paste this code:
add_shortcode('EXAMPLE','show_user_content');
function show_user_content($atts,$content = null){
global $post;
if (!is_user_logged_in()){
return "You aren’t allowed to read this. " . wp_login_url( get_permalink($post->ID) ) . ' to view the content';
}
return $content;
}
You can edit the part which returns the error if someone, who isn't logged, wants to view the content and also the EXAMPLE part in the first row - that is the name of your shortcode, so make it unique. This won't go by roles (admin, mod, editor etc) but by the state if someone is logged in.
After that just use [EXAMPLE] Some content [/EXAMPLE] in your post editor. Be aware that you have to use the text editor of the post, not the visual editor (as it doesn't recognize shortcodes).
If you want to implement this in HTML, just use the do_shortcode function:
<?php echo do_shortcode('[EXAMPLE]Hello world![/EXAMPLE]'); ?>
And just put content inside the 'Hello world!' part.
How can I place a default text (hashtag) in the Custom Message?
The textarea is (located in line 643) under jetpack/modules/publicize/ui.php
I tried to put the text in front of $title in various ways, like:
<?php echo "#myhashtag $title"; ?>
or
<?php echo '#myhashtag '.$title; ?>
but it just echoes the text, not the $title.
Any ideas will be greatly appreciated.
You can use the approach of this Wordpress plugin i made (Publicize With Hashtags), which does exactly that. It basically use and action trigger bound to the 'save_post' native event.
If you want to develop your own one you can have a look at my Source Code on the project's GitHub page or in this installation & usage guide I wrote about it.
You can add a filter, like so, to your theme's functions.php or a site-specific plugin:
add_filter( 'wpas_default_prefix', 'add_default_publicize_hashtag_prefix', 10, 4 );
function add_default_publicize_hashtag_prefix() {
$default_tags = '#yourhastaghere ';
return $default_tags;
}
This will add your default hashtag before your title without you needing to hack the WordPress core.
jetpack/modules/publicize/ui.php itself states in its comments:
/**
* Only user facing pieces of Publicize are found here.
*/
You added your hashtag in the textarea which allows admins to enter a custom message (click edit and it will slide down with your hashtag).
As #Yazmin mentioned, the best way to permanently edit the message is using a filter. The filters available are wpas_default_prefix, wpas_default_message, and wpas_default_suffix.
Personally, I had no success using these filters and I'm interested in a working solution to this issue myself.
On my wordpress blog my images link to another page and I would like to remove that link. I'm pretty sure it can't be done using Wordpress' hooks and I've been trying with preg_replace, but to no success.
So Here's an example:
This is a simple link while this is an image link <img src="">
So I only want the images' URL to be removed, while the text one remains.
Any ideas ?
Thank you
You can use regex:
*(<img src="[^"]+" */?>) *
and replace with '\1'.
You can hook the the_content filter and apply a regex with preg_replace to do that.
Here's some info and examples on using the hook: http://codex.wordpress.org/Plugin_API/Filter_Reference/the_content
wordpress , plugins : hi , I want to create website by wordpress but I want to make speacial plugins .. and I made it but there are problem .. _
I have a website and I using it with wiki for contries in the world ..
I want from the plugins 'when the visitor open the page for example 'United State' , I want from the plugins to search about all of world in this page like cities ' New York , .... ' and replace it by like for city's page for example = New York ..
I don't know how make it ..
I tried few times but I fail >_< ..
<?php
/*
Plugin Name: ReplaceMent
*/
$wiki_contry = array ("Japan"=>"<a href='?japan.php'> Japan <a/>","United State"=>"<a href='?usa.php'> United State <a/>");
function replace($text){
foreach ($wiki_contry as $ky => $vl){
$text = str_replace(ِ"$ky","$vl", $text);
return $text;
}
}
add_filter('the_content','replace');
?>
other world , I want my site like wikipidia but using wordpress and plugins ^_^..
thank you very much
I hate to be frank but your English is terrible. I can't clearly understand what you're doing or why you're trying to do it.
Why would you want your site to be like Wikipedia utilizing Wordpress and its plugins when you can just use MediaWiki for free?
Replying to quite an old post,
but .. the answer seems to be kind of missing,
so I am giving it a shot, to provide an answer.
Perhaps adding this in your function-body will help ?
global $wiki_contry;
And perhaps a less intense way to do it would be something along the lines of the content described in this article https://themefuse.com/wordpress-tutorial-how-to-link-keywords-to-urls-automatically/ . So, essentially a function that replaces the text content when it is saved, using WordPress-hook 'content_save_pre', to execute the replace function and save that text to the database.
add_filter('content_save_pre','replace_keywords_fn');
I would also like to suggest to you the idea of using tags for the countries and regions, so you can point to the overview page for that tag.