Edit

Various WP snippets

Show Links Menu w/Active Class

<nav>
<?php $args=array(
	'sort_order' => 'ASC',
	'sort_column' => 'post_date',
	'hierarchical' => 1,
	'exclude' => '',
	'include' => '',
	'meta_key' => '',
	'meta_value' => '',
	'authors' => '',
	'child_of' => 0,
	'parent' => -1,
	'exclude_tree' => '',
	'number' => '',
	'offset' => 0,
	'post_type' => 'page',
	'post_status' => 'publish'
);
$pages = get_pages($args);
$current_id = get_the_ID();
if( $pages ){
    echo( "<ul>\n" );
    foreach( $pages as $page){
        $link_page_id = (

Output a menu

<?php


$page_menu_items = menu_tree_output(menu_tree_all_data("main-menu"));

echo( drupal_render($page_menu_items) );


?>

LDAP Connect and Bind Test

<?php


$host = "127.0.0.1:10389";
$ldap = ldap_connect($host);
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);


$username = "uid=admin,ou=system";
$password = "secret";


if( $bind = ldap_bind($ldap, $username, $password) )

    echo( "logged into ".$host." as ".$username );

else

    echo( "Fail" );


?>

Create custom content type

contenttypetest.info

name = Contenttype test
description = Test - how can I create a new content type with hook form?
core = 7.x
files[] = contenttypetest.module;
files[] = contenttypetest.install;

contenttypetest.module

<?php

    // hook_form implementation
    function contenttypetest_form($node, &$form_state) {
        $form = array();


        $form['contenttypetest_pass'] = array(
            '#type' => 'password',
            '#title' => t('Type a password'),
            '#description' => t('You can type anything you 

Fill array with server request URI strings

<?php

$request = explode( "/", $_SERVER["REQUEST_URI"] );
array_shift( $request );

print_r( $requests );

?>

Render imagecache images

echo( "<img src=\"".image_style_url("thumbnail", $photo["uri"] )."\" alt=\"\"/>" );

Add unique IDs to menu items

<?php
function HOOK_menu_link( array $variables ){

    $element = $variables['element'];
    $sub_menu = '';
    $name_id = strtolower(strip_tags($element['#title']));

    // remove colons and anything past colons
    if (strpos($name_id, ':')) $name_id = substr ($name_id, 0, strpos($name_id, ':'));

    //Preserve alphanumerics, everything else goes away
    $pattern = '/[^a-z]+/ ';
    $name_id = preg_replace($pattern, '', $name_id);

    $element['#attributes']['id'][] = 'menu-' . $element['#original_link']['mlid'] .

Obtain matching entities

function obtain_entities_by_type($entity_type,$bundle,$limit=10,$offset=0,$subject_nid=FALSE){

    $query=new EntityFieldQuery();

    $query->entityCondition('entity_type', $entity_type)
        ->entityCondition('bundle', $bundle)
        ->propertyCondition('status', 1)
        ->range($offset, $limit);

    if($subject_nid)
        $query->fieldCondition('field_subject','nid',$subject_nid);

    $results=$query->execute();

    if(array_key_exists('node',$results))
        return entity_load($entity_type,array_keys($results[$entity_type]));

}

Show field when a different selection is made on the same form

function HOOK_form_alter( &$form, &$form_state, $form_id ){

    if( $form_id == "user_register_form" || $form_id == "user_profile_form" ) {


        // Shows OTHER text field when 'Other' is selected as their specialty

        $form["field_specialty_other"]["#states"] = array(
            "visible" => array(
                ':input[name="field_specialty[und]"]' => array("value"=>"Other"),
            )
        );


    }

}

Insert email as username at registration

<?php
function HOOK_user_insert( &$edit, &$account, $category = NULL ){

    // Don't create a new username if one is already set
    if( strpos($account->name, 'temporary_') !== 0)
        return;

    // Otherwise, replace username with email address field
    db_update("users")
        ->fields(array("name" => $edit["mail"]))
        ->condition("uid", $account->uid)
        ->execute();

    $edit["name"] = $edit["mail"];
    $account->name = $edit["mail"];
    return;
}


function HOOK_user_register_form_alter( &$form, &$form_state, $form_id ){

    $form["ac

Pages