PHP, Wordpress

Display Conditional Pages

I have been trying to exclude buddypress pages from wp_nav_menu.

function get_ID_by_page_name() {
global $wpdb;
$page_name = array('Activity','Forums','Groups','Members','Register','Activate');
//print_r($page_name);
for($i=0;$i<sizeof($page_name);$i++)
{
//echo $page_name[$i];
$page_name_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '".$page_name[$i]."' AND post_type = 'page'");
?>
<style>
.page-item-<?php echo $page_name_id; ?> {display:none; }
</style>
<?php
}
}
echo get_ID_by_page_name();
Advertisements
PHP, Wordpress

Global search through wordpress multisite network

<?php
$searchfor = get_search_query(); // Get the search query for display in a headline
?>
<h1>Search results for '<?php echo $searchfor; ?>'</h1>
<?php
$query_string=esc_attr($query_string); // Escaping search queries to eliminate potential MySQL-injections
$blogs = get_blog_list( 0,'all' );
$search = new WP_Query($query_string);
if ($search->found_posts>0) {
foreach ( $blogs as $blog ):
switch_to_blog($blog['blog_id']);
foreach ( $search->posts as $post ) {
setup_postdata($post);
$author_data = get_userdata(get_the_author_meta('ID'));
?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<p>
<span><?php the_time('Y/m/d') ?></span>
By <?php the_author_posts_link();?> </p>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<div id="entry-content"><?php
the_excerpt();
?>
</div>
</div>
<?php
}
endforeach;
else
{
echo "Not Found";
}
restore_current_blog(); // Reset settings to the current blog
?>
Wordpress

Remove Admin Bar from WordPress 3.3

Paste the below code in your ‘functions.php’

// Remove Admin Bar From WP 3.3
if (!function_exists('disableAdminBar')) {

	function disableAdminBar(){

  	remove_action( 'admin_footer', 'wp_admin_bar_render', 1000 ); // for the admin page
    remove_action( 'wp_footer', 'wp_admin_bar_render', 1000 ); // for the front end

    function remove_admin_bar_style_backend() {  // css override for the admin page
      echo '<style>body.admin-bar #wpcontent, body.admin-bar #adminmenu { padding-top: 0px !important; }</style>';
    }

    add_filter('admin_head','remove_admin_bar_style_backend');

    function remove_admin_bar_style_frontend() { // css override for the frontend
      echo '<style type="text/css" media="screen">
      html { margin-top: 0px !important; }
      * html body { margin-top: 0px !important; }
      </style>';
    }

    add_filter('wp_head','remove_admin_bar_style_frontend', 99);

  }

}

// add_filter('admin_head','remove_admin_bar_style_backend'); // Original version
add_action('init','disableAdminBar'); // New version
PHP, Wordpress

Convert stdClass object to Array

function objectToArray($d) {
		if (is_object($d)) {
			// Gets the properties of the given object
			// with get_object_vars function
			$d = get_object_vars($d);
		}
 		if (is_array($d)) {
			/*
			* Return array converted to object
			* Using __FUNCTION__ (Magic constant)
			* for recursive call
			*/
			return array_map(__FUNCTION__, $d);
		}
		else {
			// Return array
			return $d;
	}
	}

Call your Array variable with the function.

objectToArray(Array_variable);
Wordpress

Create user using wp_create_user

<?php

$base = dirname(__FILE__);

    require( dirname(dirname(dirname($base))) .'/wp-blog-header.php');
     
    $user_name =$_POST['signup_username'];
    $user_email = $_POST['signup_email'];
     
    $user_id = username_exists( $user_name );
    if ( !$user_id ) {
    $random_password = wp_generate_password( 12, false );
    $user_id = wp_create_user( $user_name, $random_password, $user_email );
    } else {
    $random_password = __('User already exists. Password inherited.');
    }
?>