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.
Related
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.
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)
this is how I'm going with wordpress forum
it is such that even if you do not log into the site you can see what you can create, etc.
Create forum / post to the site just being tucked away so it is only possible to view it if you are login on the page.
Comment content must also be away but just get some text that you can not write content until he has sustained itself on the page.
I purchased this forum
http://themeforest.net/item/forumengine-flat-responsive-wordpress-forum-theme/5999646
WordPress comes with all the functionality needed for this its as simple as wrapping the content you want within an if statement for example
<?php if ( is_user_logged_in() ) {
echo "Member Content";
}else{
echo "Sorry Guest";
?>
id suggest something like above if you are working with template files you could always wrap the while loop with the above
I want to write a simple plugin that calls API of a website I use and gives back list of links/urls back to me.
I am trying to follow the gazillion "write your own simple wordpress plugin" links but not getting it right.
This is what I've done:
1) created a plugin file under plugins/myplugin.php which looks something like this:
function myplugin()
{
// making api call via curl
// return the result
return $result;
}
I've activated this plugin in wordpress admin panel.
I want to call this on a specific page of my website. That page is written in php.
I am doing simple <?php $blah=myplugin(); echo "$blah"; ?>
But its not doing anything. What am I missing.
Edit:0
When I do "view selection source" on the page where I am trying to use this function, I get:
<div class="post">
<!--?php $blah=myfunction(); echo "$blah"; ?-->
</div>
Does that seem correct?
Are you sure that $result contains any information at all?
If you use echo statements inside of myplugin() that will print on your site.
Because from what I can see you're not doing anything wrong.
Also, obviously, make sure that you call your plugin from the current template.
You may find this weird, actually very weird, but is the following possible?
just load up a post or page into the admin panel and place something like
this into the TINYMCE editor;
[?php
//assuming exec-PHP is ready and enabled
$current_page_url_here = get_current_url();
echo "<a href='{$current_page_url_here}?var1=val1'>click me</a>";
if ( $_GET['var1']=='val1' )
{
//change the current post's html title to <title>val1</title>
without using javascript/jquery
}
?]
so when the visitor, clicks on the "click me" link, the page reloads but
this time, the html title reads "val1".
Is such a thing technically possible?
Or is it too late ( as I expect ) at that time to make a change into the already calculated the_title() function return value?
Could ob_start in any shape or form be deployed here to achieve this goal?
Since search engine aspect is important, I cannot take a JS/JQuery ( client side ) solution as an answer.
Yes I think it might be possible ... u will run the above if statement in header.php and assign the different titles depending on the results....take a look at twentyeleven wordpress theme header.php there they change the site description on what's being viewed.