How can I display Product Images to Logged In Users only, with Logged Out Users seeing an alternative image? - wordpress

I would like to modify my WordPress/WooCommerce website, so that my Product Images have the following conditions:
Logged in site visitors:
I would like logged in visitors, to be able to see all of the WooCommerce Product Images.
Logged out site visitors:
I would like logged out visitors, to see an alternative image. Therefore, hide the Product Image. This alternative image, will be the same for every Product.

Firstly, you are going to need to create a Child Theme, for your WordPress/WooCommerce website.
Once created, simply insert the following code, into the functions.php file, within your Child Theme:
<?php
// Remove Default WooCommerce Product Images/Placeholders.
remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_images', 20 );
// Create conditional logic function.
function logged_in_images(){
// If site visitor is Logged in, display Default WooCommerce Product Images/Placeholder.
if ( is_user_logged_in() ) {
echo woocommerce_show_product_images();
}
// If site visitor is not logged in, display the following Image instead.
else {
echo '<img src=http://vaporcenter.be/theme/Wholesale/img/placeholders/default.jpg?1504084579>';
};
}
// We are inserting ('Hooking' into) the above function into where the Default WooCommerce Product Image/Placeholder was, before we removed it.
add_action( 'woocommerce_before_single_product_summary', 'logged_in_images', 20 );
?>
Be sure to replace http://vaporcenter.be/theme/Wholesale/img/placeholders/default.jpg?1504084579 with your desired image.
Don't forget, you can remove the comments (// ...) from the code. They are there, to just help explain what is happening.
I have tested the above code and it does work.
Best of luck!
If you receive an error
Step One: Using an FTP client, such as Filezilla, go into your Root Folder (Where you installed WordPress).
Look for a file entitled wp-config.php and drag this to your computer.
Open the file, using Notepad/Notepad++ and look for the line define('WP_DEBUG', false); and change 'false' to 'true'.
Drag the wp-config.php file back from where you got it from.
Refresh the page you are having an issue. You should then see an error message, which will be helpful in addressing how to resolve any issues.
Once you have resolved your issue, and your website is back in working order, head back into your wp-config.php and change 'true' back to 'false'.

Related

Wordpress errors from shortcodes being executed on edit page

I am writing a wordpress plugin.
I have noticed in my debug log that i get a lot of PHP errors thrown because my shortcodes are being executed on the admin edit pages and therefore the relevant data is not available because the shortcode is loading dynamic data based upon the user front end. For example i have a function:
function myFunction_availability() {
if (is_admin()) return; // tried adding this but still get the issue
$product = $this->myFunction_get_current_product();
return "<div class='product-availability'>{$product->availability}</div>";
}
Works fine from the front end, but whenever i load the edit page from admin area i get in my log:
PHP Warning: Attempt to read property "availability" on null in /home/vagrant/yodahwp/wp-content/plugins/yodah/class.yodah.php on line 1602
As you can see from my code, i tried adding is_admin() to exit out of the function if viewing an admin page (i.e. the edit post page) but this does not seem to work.
Do any wordpress whizzes have an answer for this? I am a bit surprised that shortcodes are executed on the admin edit pages, or am I missing something?!
Thanks
This is an old question. Usually, this happens when using visual builders.
One solution is to write a condition to check if the product exists.
If using woocommerce you can try:
$product = wc_get_product( get_the_ID() );
if($product){
//continue
}
In your case, you should edit your myFunction_get_current_product() method and verify there if the product exists.

WordPress functions.php - Admin html injection and submitting forms

I created a new navigation item on the left for my WP Admin:
add_action( 'admin_menu', 'addManagementMenuItem' );
function addManagementMenuItem(){
add_menu_page('Issue Management', 'Issue Management', 'manage_options', 'issue_management_slug', 'issue_management_building_function','',3);
}
function issue_management_building_function(){
if(!current_user_can('manage_options')){
}
else {
?>
...
...
So where I have the ellipsis ... is where my HTML begins and I write out some information to the page with various php echo statements to print some data out.
What I would like to do is now give the user the ability to enter in a filter and press submit. This would issue a POST to another page which would receive the post data, run some stuff, and spit out something else to the screen. I was just thinking this would take the user away from the WP-ADMIN area entirely (what I want to do is keep the user all within the right pane so it looks like it's natively happening on WordPress under my new admin area)
Something feels wrong about this approach above where I'm putting tons of html into functions.php - what is the way to create pages for a custom admin section where I can do things like post forms and go to multiple pages?
I was thinking the best solution would be to put an iframe in my injected HTML in functions.php, and then the pages can talk to themselves just like normal behind the scenes in WP-admin.
Could anyone point me in the right direction?
thanks!
Considering the user input/_POST features you'd like to add to this, you may want to consider building this functionality out as your own plugin. I've always kept custom functionality limited to non-user interaction in the functions.php file, but anything further would probably be better fit as it's own plugin.
For example, what if you created a plugin directory named nullhypothesis:
add_action( 'admin_menu', 'addManagementMenuItem' );
function addManagementMenuItem(){
add_menu_page('Issue Management', 'Issue Management', 'manage_options', 'nullhypothesis/file_to_do_your_bidding.php', 'issue_management_building_function','',3);
}
It's that fourth parameter that in the documentation mentions that you should include the menu_slug, but it doesn't necessarily need to only be a function - it can also be a file you define.
Then, in your file_to_do_your_bidding.php file (within your plugin), you can add whatever _POST functionality you'd need it to. It could also exist as the 'admin' page that the administrator/whoever interacts with.
Was that what you were looking for?

Wordpress: On specific url the admin toolbar shows up (Even when not logged in)

I'm running into a Major trouble.
Today I found out that when visiting a specific URL on my website, the admin toolbar shows up. Also for people who are not logged in. It appears that the visitor is logged in, but that's not true. All the links in the toolbar go to a 404 page. However, important information is being shown, like how many plugins need an update and even my login name.
Is anyone familiar with this?
Since it is a corporate website, I need a fix as soon as possible.
(due to security reasons I won't post the exact url in public)
Hopefully anyone can help me.
Thanks,
Stefan
Try to add either of these code in functions.php of your theme,
Code 1:
<?php
add_filter('show_admin_bar', '__return_false');
?>
Code 2:
function hide_admin_bar_from_front_end(){
remove_action( 'wp_head', '_admin_bar_bump_cb' );
return false;
}
add_filter( 'show_admin_bar', 'hide_admin_bar_from_front_end' );

Hide Wordpress dashboard?

I'm new to wordpress and a bit confused with something. I'm trying to build a classified marketplace type of website for myself. I am NOT building this for a "client". I will probably be using a hack of several different plugins as my coding skills are not up to par. Eventually I will hopefully have lots of users who will be composed of buyers & sellers.
My question pertains to the WP dashboard. When buyers/sellers sign up for my site, will they be able to see the backend WP dashboard? I would prefer that they NOT be able to access a backend dashboard at all let alone a WP branded one. Is this possible? If so any clue as to how this might be accomplished?
thank you Brian
Normal users do not actually see the 'backend' WP dashboard. What they are seeing is a 'profile' type page meant for the original functionality of wordpres; being a blog.
If you do not want users to go to this page when they log-in, you can use a couple of hooks. Here is some code that redirects to the front page after logging-in and logging-out. This goes in your functions.php file.
add_action('login_form', 'ang_redirect_to_front_page');
add_action('wp_logout', 'go_home');
function ang_redirect_to_front_page() {
global $redirect_to;
if (!isset($_GET['redirect_to'])) {
$redirect_to = get_option('siteurl');
}
}
function go_home(){
wp_redirect( home_url() );
exit();
}
And, if your theme is still displaying the menu at the top of the screen that allows the users to go to this 'profile' area, you can go into your footer.php file and remove this:
<?php wp_footer();?>
However, if you do this, then you will not see it as the admin either.
WordPress is might not be the thing to use for that kind of website, even with a bunch of plugins. Read up on other content management systems just incase.
This link might answer your question:
http://buddypress.org/support/topic/how-to-prevent-non-admins-from-accessing-wp-admin-dashboard/
You can also add this to your theme's function.php file:
// DISABLE ADMIN BAR FOR ALL USERS
show_admin_bar( false );
If you are not too used to wordpress, use WOOCOMMERCE plugin. Its completely free and well documented

PHP echo function in BuddyPress to Hyperlink to Member Profile Pages

When I first setup BuddyPress on my site, root profiles were enabled. Basically, if a registered user wanted to edit the account settings of their profile, the URL where they could do that would be: "domain.com/username". However, if you typed in "domain.com/members/username" in your browser, you would get the exact same page.
I decided I did not want root profiles enabled because I preferred the URL to be: "domain.com/members/username", so based on this guide (http://codex.buddypress.org/extending-buddypress/changing-internal-configuration-settings), I edited the code in functions.php to be like this:
define ( 'BP_ENABLE_ROOT_PROFILES', false );
However, in header.php, where my navigation is located, the PHP function is still linking to the root profile that I wanted disabled. This is what it looks like:
Profile
I am trying to figure out how I can change the function so that it links to: "domain.com/members/username" instead of "domain.com/username".
Please remove the define ( 'BP_ENABLE_ROOT_PROFILES', false ); in your function.php file.
Because the Buddypress default provide domain.com/members/username.
And also please check the wp-config.php file if above code is there just remove it
Please try this

Resources