I am trying to use the following code to 1st check if a user has a certain member level, then if they have a blog on the wp network. If they pass both those checks then a link is echoed, if they dont pass the first if check then another link is echoed. Also though, I am trying to check if they pass the first if but fail the second one then a different link is echoed. Here's the code I have now -
<?php
if(pmpro_hasMembershipLevel(array(2,4))) {
if(current_user_can( 'edit_posts' )) {
global $current_user;
$blogs = get_blogs_of_user( $current_user->id );
if($blogs) {
foreach ( $blogs as $blog ) {
if($blog->userblog_id != 1) {
echo '<li>My Site</li>';
} else {
echo '<li>Register your Site</li>';
}
}
}
}
} else {
echo '<li>UPGRADE</li>';
}
?>
The code above echoes the register link when its suppose to but when the user has a blog, the register link shouldnt show but now it shows next to my site link. Any ideas?
EDIT
Free user sees a UPGRADE link
Premium Users without site see a REGISTER Link ( the membership array of 2,4 are the levels they have to be either one of )
Premium members with a site will see the MY SITE link.
EDIT
I was able to use the print_r and on the page where it's suppose to echo the register link -- Array ( [1] => stdClass Object ( [userblog_id] => 1 [blogname] => mysite.com [domain] => mysite.com [path] => / [site_id] => 1 [siteurl] => https://mysite.com [archived] => 0 [spam] => 0 [deleted] => 0 ) )
Looking at the Wordpress MU documentation, I would guess that the get_blogs_of_user always returns an array, so checking on the value of $blogs exists is always going to return true. In the following code, I suggest replacing the simple check on the existence of a value with a check to determine if the returned value is an array and, if so, whether it has elements or not:
<?php
if (pmpro_hasMembershipLevel(array(2,4))) {
if (current_user_can( 'edit_posts' )) :
global $current_user;
$blogs = get_blogs_of_user( $current_user->id );
/*Check if we got an array back and, if so,
check if it has elements*/
if ( is_array($blogs) && ( count($blogs) > 0 ) ) {
foreach ( $blogs as $blog ) :
if($blog->userblog_id != 1) {
echo '<li><a href="http://' . $blog->domain
. $blog->path
.'wp-admin/">My Site</a></li>';
}
endforeach; // end foreach loop
} else {
echo 'Register your Site';
} // end if $blogs
endif; // endif current_user_can
} else {
?>
<div>UPGRADE</div>
<?php
}
?>
Try this :
<?php if(pmpro_hasMembershipLevel(array(2,4))) {
if(current_user_can( 'edit_posts' )) {
global $current_user;
$blogs = get_blogs_of_user( $current_user->id );
if($blogs) {
foreach ( $blogs as $blog ) {
if($blog->userblog_id != 1) {
echo '<li>My Site</li>';
}
}
} else {
echo 'Register your Site';
}
}
} else { ?>
<div>UPGRADE</div>
<?php } ?>
Give this one a shot. Even if it doesn't work in its current state, it should be easier to see the logic and figure out whats not working properly.
EDIT: Shamelessly stole #JustinPearce's method of checking if the user has a blog from his answer
<?php
global $current_user;
$blogs = get_blogs_of_user( $current_user->id );
// print_r($blogs);
$has_membership_level = pmpro_hasMembershipLevel(array(2,4));
$has_blog = ( current_user_can('edit_posts') && is_array($blogs) && count($blogs) > 0 );
$registerLink = 'Register your Site';
$upgradeLink = '<div>UPGRADE</div>';
function echoBlogLinks($blogs) {
echo '<ul>';
foreach ( $blogs as $blog ) {
if($blog->userblog_id != 1) {
echo '<li>My Site</li>';
}
}
echo '</ul>';
}
if ($has_membership_level) {
if ($has_blog) {
echoBlogLinks($blogs);
} else {
echo $registerLink;
}
} else {
echo $upgradeLink;
}
Related
I'd want to edit the content of one of my posts using this way, but it's not working.
Does this filter affect the php output or the raw php file?
add_filter( 'the_content', 'multiple_string_replacements');
function multiple_string_replacements ( $contentt ) {
if ( is_single('15467') && in_the_loop() && is_main_query() ) {
$condition = true;
if($condition){
$urlsdothejob = "link.com";
$text = array(
"$variable1" => "$urlsdothejob",
"$variable2" => "$urlsdothejob",
);
$contentt = str_ireplace(array_keys( $text ), $text, $contentt);
};
}
return $contentt;
}
If you know the conditions that you're looking for, you should be able to write an IF statement to match.
if ($constantvalue == "anothervalue") {
// make local changes
}
Can you give an example of what you're trying to do? It's hard to understand the request with such little information.
Try like this hope its working for you.
while(have_posts()):
$ID = get_the_ID();
$data1 = get_post_meta($ID,'meta_key',true);
$data2 = get_post_meta($ID,'meta_key',true);
if($data1 == $data2) {
update_post_meta($ID,'meta_key','meta_value');
}
endwhile;
I need to collect data through a contact form and track customer referrals. I am creating a tag for the form, but I need it to be filled only if there was a transition from a referral that contained partner
my code
// Original Referrer
function wpshore_set_session_values()
{
if (!session_id())
{
session_start();
}
if (!isset($_SESSION['OriginalRef']))
{
$_SESSION['OriginalRef'] = $_SERVER['HTTP_REFERER'];
}
if (!isset($_SESSION['LandingPage']))
{
$_SESSION['LandingPage'] = $_SERVER["REQUEST_URI"];
}
}
add_action('init', 'wpshore_set_session_values');
///
function getRefererPage3( $form_tag ){
$partner = array (
'/partner/',
);
$parts = parse_url($_SERVER['HTTP_REFERER'];
if (!empty($parts['path']) || in_array($parts['path'], $partner)){ //this row. I doubt how it should be
if ( $form_tag['name'] == 'referer-page3' ) {
$form_tag['values'][] = $_SESSION['LandingPage'];
}
}
return $form_tag;
}
if ( !is_admin() ) {
add_filter( 'wpcf7_form_tag', 'getRefererPage3' );
}
I found my problem. Maybe this solution will help anyone in the future.
function getRefererPage3( $form_tag ){
if (strpos($_SESSION['LandingPage'], 'partner') !== false) {
if ( $form_tag['name'] == 'referer-page3 ) {
$form_tag['values'][] = $_SESSION['LandingPage'];
}
}
return $form_tag;
}
if ( !is_admin() ) {
add_filter( 'wpcf7_form_tag', 'getRefererPage3' );
}
I am trying to check if a user is logged in as a member and also if a meta field in a WordPress custom post type has content and then need to display content based on 4 possible outcomes. I am using WordPress membership plugin woomembers
user is logged in as a member and content exists - display content
user is logged in as a member and content field is empty - display nothing
user is not a member and content exists - display some content and sign up
user is not a member and no content exists - display sign up
I have this code partly working but can't get item 3 to work?
The code I have is:
<?php $meta_content_field = get_post_meta($post->ID, "meta-content-field",
$single = true);
if (wc_memberships_get_user_active_memberships() &&
$meta_lighting_diagram != '') {
// Active member and has content- do something here
?>
html content here
<?php
} elseif (wc_memberships_get_user_active_memberships() &&
empty($meta_content_field) ) {
// No content but active member - do something here
echo "No content but active member";
} elseif ( ! empty( wc_memberships_get_user_active_memberships() ) &&
($meta_content_field) ) {
// content but non-member - do something here - this is not working?
echo "Has content but NOT active member";
} else {
// Non-member - do something here ?>
html content here
<?php }
?>
Your loop is wrong. First check should be member or not.
if ( $member) {
// do something for the member
} else {
// do something for the non-member
}
Then you want to check whether or not there is content.
Do this within the if-loop
if ( $member ) {
// do something for the member
if ( $content ) {
// member and content
} else {
// member no content
}
} else {
// do something for the non-member
if ( $content ) {
// non-member and content
} else {
// non-member no content
}
}
a maybe even better approach would be to use switch(). Could look like this:
$status = '';
if ( $member ) $status = 'member';
else $status = 'nonmember';
if ( $content ) $status .= 'content';
else $status .= 'nocontent';
switch ( $status ) {
case 'membercontent':
// do something
break;
case 'membernocontent':
// do something
break;
case 'nonmembercontent':
// do something
break;
case 'nonmembernocontent':
// do something
break;
}
This can be simplified (shortened) in combination with default:.
This code helps me show the data stored in the reviews array but whenever i try calling the variables individually i get an error. So how can i display the variables indiviually.
<?php $reviews = Mage::getModel('review/review')->getResourceCollection();
$reviews->addStoreFilter( Mage::app()->getStore()->getId() )
->addStatusFilter( Mage_Review_Model_Review::STATUS_APPROVED )
->setDateOrder()
->addRateVotes()
->load();
print_r($reviews->getData());
This is line where i get the error:
echo $reviews->getTitle();
?>
the error is Fatal error: Call to undefined function. Please help.
I have never used Magento, but I think you have to iterate through reviews. Try this.
<?php
$reviews = Mage::getModel('review/review')->getResourceCollection()
->addStoreFilter( Mage::app()->getStore()->getId() )
->addStatusFilter( Mage_Review_Model_Review::STATUS_APPROVED )
->setDateOrder()
->addRateVotes();
if (count($reviews) > 0) {
foreach ($reviews->getItems() as $review) {
// Individual review
echo $review->getTitle();
}
}
?>
$ReviewsCollection = $this->getReviewsCollection()->getItems();
if( count( $ReviewsCollection ) ) {
$x = 1;
foreach( $ReviewsCollection as $_review ){
// get review title
echo $_review->getTitle();
$_votes = $_review->getRatingVotes();
if (count($_votes)){
foreach ($_votes as $_vote){
echo $_vote->getRatingCode().' - '.$_vote->getPercent().'%<br />';
}
}
// get review date
echo $this->formatDate($_review->getCreatedAt()).'<br />';
// get review detail
echo $_review->getDetail().'<br />';
// get how wrote the review
echo $_review->getNickname().'<br />';
// because you cant setPageSize with this collection it's best to break the loop.
if($x == 4){ break; }
$x++;
}
}
I have the following php code.
Im trying to find out the field name/key that does not exist without using hardcoded static text. Is there a php function that can do this. array_keys_exist is similar to what I want but it only allows for checking of a single key.
ex:
Something like
$keys_to_confirm = ['password','password_old',....];
$user_submitted_input_array = ['password'=>'...', 'somekey'=>'...' ];
bool : all_array_keys_exist($keys_to_confirm,$user_submitted_input_array);
Current code that uses static text to report the missing field/key name
if( isset($input['password']))
{
if( isset($input['password_old'])) )
{
if(isset($input['password_repeat'])) )
{
//good to go!
}
else
{
die('missing form element password_repeat.');
}
}
else
{
die('missing form element password_old.');
}
}
else
{
die('missing form element password.');
}
I had this code but it doesnt say what is missing.
//if the field doesnt exist for some reason lets create a dummy
if( ! isset($input['password'])
OR (!isset($input['password_repeat']))
OR (!isset($input['password_old'])) )
{
die('missing form element.');
}
Try:
foreach( array('password', 'password_repeat', 'password_old') as $key ) {
if( empty($input[ $key ]) ) {
echo 'Missing field: ' . $key;
}
}
You can probably come up with a one liner to do this using array_diff_key, but I think the above is concise enough.
Here's the one liner with array_key_diff. It's not the prettiest but functional.
$keys_to_confirm = array('password' => '','password_old' => '');
$user_submitted_input_array = array('password'=>'...', 'somekey'=>'...');
$validate = array_diff_key($keys_to_confirm, $user_submitted_input_array);
if( empty($validate) ) {
echo 'Passed';
} else {
echo 'Missing field: ' . key($validate);
}
My solution:
if ($field_miss = array_diff_key(array('password', 'password_old', '...'), array_keys($input))) {
echo 'Missing field: ', implode(', ', $field_miss);
}