Skip to content

Instantly share code, notes, and snippets.

@airton
Last active February 26, 2016 15:08
Show Gist options
  • Save airton/9604620 to your computer and use it in GitHub Desktop.
Save airton/9604620 to your computer and use it in GitHub Desktop.
Functions Wordpress
<?php
/* ==========================================================================
CUSTOMIZANDO MENU PRINCIPAL
========================================================================== */
class custom_menu extends Walker_Nav_Menu{
function start_lvl(&$output, $depth) {
$indent = str_repeat("", $depth);
$output .= "<i class=\"icon-caret-down\"></i><ul class=\"nav-submenu\" role=\"menu\" aria-hidden=\"true\">";
}
function start_el(&$output, $item, $depth, $args){
global $wp_query;
$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
$class_names = $value = '';
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$class_names = in_array("current_page_item",$item->classes) ? 'current' : '';
$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
$output .= $indent . '<li class="item'.$item->ID.' '.$value.$class_names.'" aria-haspopup="false" role="menuitem">';
$attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : '';
$item_output = $args->before;
$item_output .= '<a'. $attributes .'>';
$item_output .= $args->link_before .apply_filters( 'the_title', $item->title, $item->ID );
$item_output .= '</a>';
$item_output .= $args->after;
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}
}
/* ==========================================================================
CUSTOMIZANDO MENU INTERNO SIDEBAR
========================================================================== */
class custom_sidebar extends Walker_Nav_Menu{
function start_lvl(&$output, $depth) {
$indent = str_repeat("", $depth);
$output .= "<ul class=\"side-sub\">";
}
function start_el(&$output, $item, $depth, $args){
global $wp_query;
$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
$class_names = $value = '';
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$class_names = in_array("current_page_item",$item->classes) ? 'current' : '';
$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
$output .= $indent . '<li class="item'.$item->ID.' '.$value.$class_names.'">';
$attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : '';
$item_output = $args->before;
$item_output .= '<a'. $attributes .'>';
$item_output .= $args->link_before .apply_filters( 'the_title', $item->title, $item->ID );
$item_output .= '</a>';
$item_output .= $args->after;
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}
}
/* ==========================================================================
CUSTOM EXCERPT
========================================================================== */
function excerpt($limit) {
$excerpt = explode(' ', get_the_excerpt(), $limit);
if (count($excerpt)>=$limit) {
array_pop($excerpt);
$excerpt = implode(" ",$excerpt).'...';
} else {
$excerpt = implode(" ",$excerpt);
}
$excerpt = preg_replace('`[[^]]*]`','',$excerpt);
return $excerpt;
}
/* ==========================================================================
CUSTOM CONTENT
========================================================================== */
function content($limit) {
$content = explode(' ', get_the_content(), $limit);
if (count($content)>=$limit) {
array_pop($content);
$content = implode(" ",$content).'...';
} else {
$content = implode(" ",$content);
}
$content = preg_replace('/[.+]/','', $content);
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]&gt;', $content);
return $content;
}
/* ==========================================================================
REMOVER POST MENU DO ADMIN
========================================================================== */
add_action( 'admin_menu', 'my_remove_menu_pages' );
function my_remove_menu_pages() {
remove_menu_page('edit.php');
}
/* ==========================================================================
REMOVE COLUNAS DE LISTA DE POSTS E CUSTOM POSTS
========================================================================== */
function remove_post_columns($defaults) {
unset($defaults['author']); //REMOVE COLUNA DE AUTOR
unset($defaults['comments']); //REMOVER CULUNA DE COMENTARIOS
unset($defaults['date']); //REMOVER COLUNA DE DATA
return $defaults;
}
add_filter('manage_{POST-NAME}_posts_columns', 'remove_post_columns');
/* ==========================================================================
ADICIONAR SCRIPTS NO ADMIN
========================================================================== */
function admin_scripts() {
wp_register_script('my-upload', get_template_directory_uri() . '/assets/js/admin.js');
wp_enqueue_script('my-upload');
}
add_action('admin_print_scripts', 'admin_scripts');
/* ==========================================================================
ADICIONAR CUSTOM FIELD IN MIDIAS
========================================================================== */
function mytheme_attachment_fields( $fields, $post ) {
$meta = get_post_meta($post->ID, 'credito_imagem', true);
$fields['credito_imagem'] = array(
'label' => 'Crédito',
'input' => 'text',
'value' => $meta,
'show_in_edit' => true,
);
return $fields;
}
add_filter( 'attachment_fields_to_edit', 'mytheme_attachment_fields', 10, 2 );
/**
* Update custom field on save
*/
function mytheme_update_attachment_meta($attachment){
global $post;
update_post_meta($post->ID, 'credito_imagem', $attachment['attachments'][$post->ID]['credito_imagem']);
return $attachment;
}
add_filter( 'attachment_fields_to_save', 'mytheme_update_attachment_meta', 4);
/**
* Update custom field via ajax
*/
function mytheme_media_xtra_fields() {
$post_id = $_POST['id'];
$meta = $_POST['attachments'][$post_id ]['credito_imagem'];
update_post_meta($post_id , 'credito_imagem', $meta);
clean_post_cache($post_id);
}
add_action('wp_ajax_save-attachment-compat', 'mytheme_media_xtra_fields', 0, 1);
/* ==========================================================================
ADICIONAR FILTRO DE CATEGORIA EM COMBO SELECT
========================================================================== */
if (!class_exists('Tax_CTP_Filter')){
/**
* Tax CTP Filter Class
* Simple class to add custom taxonomy dropdown to a custom post type admin edit list
* @author Ohad Raz <[email protected]>
* @version 0.1
*/
class Tax_CTP_Filter
{
/**
* __construct
* @author Ohad Raz <[email protected]>
* @since 0.1
* @param array $cpt [description]
*/
function __construct($cpt = array()){
$this->cpt = $cpt;
// Adding a Taxonomy Filter to Admin List for a Custom Post Type
add_action( 'restrict_manage_posts', array($this,'my_restrict_manage_posts' ));
}
/**
* my_restrict_manage_posts add the slelect dropdown per taxonomy
* @author Ohad Raz <[email protected]>
* @since 0.1
* @return void
*/
public function my_restrict_manage_posts() {
// only display these taxonomy filters on desired custom post_type listings
global $typenow;
$types = array_keys($this->cpt);
if (in_array($typenow, $types)) {
// create an array of taxonomy slugs you want to filter by - if you want to retrieve all taxonomies, could use get_taxonomies() to build the list
$filters = $this->cpt[$typenow];
foreach ($filters as $tax_slug) {
// retrieve the taxonomy object
$tax_obj = get_taxonomy($tax_slug);
$tax_name = $tax_obj->labels->name;
// output html for taxonomy dropdown filter
echo "<select name='".strtolower($tax_slug)."' id='".strtolower($tax_slug)."' class='postform'>";
echo "<option value=''>Show All $tax_name</option>";
$this->generate_taxonomy_options($tax_slug,0,0,(isset($_GET[strtolower($tax_slug)])? $_GET[strtolower($tax_slug)] : null));
echo "</select>";
}
}
}
/**
* generate_taxonomy_options generate dropdown
* @author Ohad Raz <[email protected]>
* @since 0.1
* @param string $tax_slug
* @param string $parent
* @param integer $level
* @param string $selected
* @return void
*/
public function generate_taxonomy_options($tax_slug, $parent = '', $level = 0,$selected = null) {
$args = array('show_empty' => 1);
if(!is_null($parent)) {
$args = array('parent' => $parent);
}
$terms = get_terms($tax_slug,$args);
$tab='';
for($i=0;$i<$level;$i++){
$tab.='--';
}
foreach ($terms as $term) {
// output each select option line, check against the last $_GET to show the current option selected
echo '<option value='. $term->slug, $selected == $term->slug ? ' selected="selected"' : '','>' .$tab. $term->name .' (' . $term->count .')</option>';
$this->generate_taxonomy_options($tax_slug, $term->term_id, $level+1,$selected);
}
}
}//end class
}//end if
new Tax_CTP_Filter(array('CUSTOM_POST_TYPE_NAME' => array('CUSTOM_TAXONOMY_NAME1','CUSTOM_TAXONOMY_NAME2')));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment