Duplicate div in php - php

I created this PHP code in WordPress to add image to the menu, but it is duplicating a div where there is an add button and I don't know why, I need it to be only one and not two results of the div, in case I only want the left div... I'll add an image about what I'm talking about so you can analyze my problem!
Structure is placed inside function wp_post_thumbnail_only_html
I left only the action that passes the button to facilitate.
<?php
if ( !defined( 'ABSPATH' ) ) {
exit;
}
class SNAP_Menu_Image{
public function __construct() {
add_action( 'admin_init', array( $this, 'admin_init' ), 99 );
}
public function admin_init() {
if ( !has_action( 'wp_nav_menu_item_custom_fields' ) ) {
}
add_action( 'wp_nav_menu_item_custom_fields', array( $this, 'menu_item_custom_fields' ), 10, 4 );
}
public function file_is_displayable_image( $result, $path ) {
if ( $result ) {
return true;
}
$fileExtension = pathinfo( $path, PATHINFO_EXTENSION );
return in_array( $fileExtension, $this->additionalDisplayableImageExtensions );
}
public function menu_image_init() {
add_post_type_support( 'nav_menu_item', array( 'thumbnail' ) );
$this->image_sizes = apply_filters( 'menu_image_default_sizes', $this->image_sizes );
if (is_array($this->image_sizes)) {
foreach ($this->image_sizes as $name => $params) {
add_image_size($name, $params[0], $params[1], ( array_key_exists(2, $params) ? $params[2] : false ) );
}
}
load_plugin_textdomain( 'menu-image', false, basename( dirname( __FILE__ ) ) . '/languages' );
}
public function menu_image_nav_menu_manage_columns( $columns ) {
return $columns + array( 'image' => __( 'Image', 'menu-image' ) );
}
public function menu_image_save_post_action( $post_id, $post ) {
$menu_image_settings = array(
'menu_item_image_size',
'menu_item_image_title_position'
);
foreach ( $menu_image_settings as $setting_name ) {
if ( isset( $_POST[ $setting_name ][ $post_id ] ) && !empty( $_POST[ $setting_name ][ $post_id ] ) ) {
if ( $post->{"_$setting_name"} != $_POST[ $setting_name ][ $post_id ] ) {
update_post_meta( $post_id, "_$setting_name", esc_sql( $_POST[ $setting_name ][ $post_id ] ) );
}
}
}
}
public function wp_update_nav_menu_item_action( $item_menu_id, $menu_item_db_id ) {
global $sitepress, $icl_menus_sync;
if ( class_exists( 'SitePress' ) && $sitepress instanceof SitePress && class_exists( 'ICLMenusSync' ) && $icl_menus_sync instanceof ICLMenusSync ) {
static $run_times = array();
$menu_image_settings = array(
'menu_item_image_size',
'menu_item_image_title_position',
'thumbnail_id',
'thumbnail_hover_id',
);
foreach ( $icl_menus_sync->menus as $menu_id => $menu_data ) {
if ( !isset( $_POST[ 'sync' ][ 'add' ][ $menu_id ] ) ) {
continue;
}
$cache_key = md5( serialize( array( $item_menu_id, 'tax_nav_menu' ) ) );
$cache_group = 'get_language_for_element';
wp_cache_delete( $cache_key, $cache_group );
$lang = $sitepress->get_language_for_element( $item_menu_id, 'tax_nav_menu' );
if ( !isset( $run_times[ $menu_id ][ $lang ] ) ) {
$run_times[ $menu_id ][ $lang ] = 0;
}
$post_item_ids = array();
foreach ( $_POST[ 'sync' ][ 'add' ][ $menu_id ] as $id => $lang_array ) {
if ( array_key_exists( $lang, $lang_array ) ) {
$post_item_ids[ ] = $id;
}
}
if ( !array_key_exists( $run_times[ $menu_id ][ $lang ], $post_item_ids ) ) {
continue;
}
$orig_item_id = $post_item_ids[ $run_times[ $menu_id ][ $lang ] ];
$orig_item_meta = get_metadata( 'post', $orig_item_id );
foreach ( $menu_image_settings as $meta ) {
if ( isset( $orig_item_meta[ "_$meta" ] ) && isset( $orig_item_meta[ "_$meta" ][ 0 ] ) ) {
update_post_meta( $menu_item_db_id, "_$meta", $orig_item_meta[ "_$meta" ][ 0 ] );
}
}
$run_times[ $menu_id ][ $lang ] ++;
break;
}
}
}
public function menu_image_edit_nav_menu_walker_filter() {
return 'Menu_Image_Walker_Nav_Menu_Edit';
}
public function menu_image_wp_setup_nav_menu_item( $item ) {
if ( !isset( $item->thumbnail_id ) ) {
$item->thumbnail_id = get_post_thumbnail_id( $item->ID );
}
if ( !isset( $item->thumbnail_hover_id ) ) {
$item->thumbnail_hover_id = get_post_meta( $item->ID, '_thumbnail_hover_id', true );
}
if ( !isset( $item->image_size ) ) {
$item->image_size = get_post_meta( $item->ID, '_menu_item_image_size', true );
}
if ( !isset( $item->title_position ) ) {
$item->title_position = get_post_meta( $item->ID, '_menu_item_image_title_position', true );
}
return $item;
}
public function menu_image_nav_menu_link_attributes_filter( $atts, $item, $args, $depth = null ) {
$this->setProcessed( $item->ID );
$position = $item->title_position ? $item->title_position : apply_filters( 'menu_image_default_title_position', 'after' );
$class = ! empty( $atts[ 'class' ] ) ? $atts[ 'class' ] : '';
$class .= " menu-image-title-{$position}";
if ( $item->thumbnail_hover_id ) {
$class .= ' menu-image-hovered';
} elseif ( $item->thumbnail_id ) {
$class .= ' menu-image-not-hovered';
}
if ( ! empty( $args->walker ) && class_exists( 'FlatsomeNavDropdown' ) && $args->walker instanceof FlatsomeNavDropdown && ! is_null( $depth ) && $depth === 0 ) {
$class .= ' nav-top-link';
}
$atts[ 'class' ] = trim( $class );
return $atts;
}
public function menu_image_nav_menu_item_title_filter( $title, $item, $depth = null, $args = null) {
if (is_numeric($item)) {
$item = wp_setup_nav_menu_item( get_post( $item ) );
}
$image_size = $item->image_size ? $item->image_size : apply_filters( 'menu_image_default_size', 'menu-36x36' );
$position = $item->title_position ? $item->title_position : apply_filters( 'menu_image_default_title_position', 'after' );
$class = "menu-image-title-{$position}";
$this->setUsedAttachments( $image_size, $item->thumbnail_id );
$image = '';
if ( $item->thumbnail_hover_id ) {
$this->setUsedAttachments( $image_size, $item->thumbnail_hover_id );
$hover_image_src = wp_get_attachment_image_src( $item->thumbnail_hover_id, $image_size );
$margin_size = $hover_image_src[ 1 ];
$image = "<span class='menu-image-hover-wrapper'>";
$image .= wp_get_attachment_image( $item->thumbnail_id, $image_size, false, "class=menu-image {$class}" );
$image .= wp_get_attachment_image(
$item->thumbnail_hover_id, $image_size, false, array(
'class' => "hovered-image {$class}",
'style' => "margin-left: -{$margin_size}px;",
)
);
$image .= '</span>';;
} elseif ( $item->thumbnail_id ) {
$image = wp_get_attachment_image( $item->thumbnail_id, $image_size, false, "class=menu-image {$class}" );
}
$none = ''; // Sugar.
switch ( $position ) {
case 'hide':
case 'before':
case 'above':
$item_args = array( $none, $title, $image );
break;
case 'after':
default:
$item_args = array( $image, $title, $none );
break;
}
$title = vsprintf( '%s<span class="menu-image-title">%s</span>%s', $item_args );
return $title;
}
public function menu_image_nav_menu_item_filter( $item_output, $item, $depth, $args ) {
if ( $this->isProcessed( $item->ID ) ) {
return $item_output;
}
$attributes = !empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) . '"' : '';
$attributes .= !empty( $item->target ) ? ' target="' . esc_attr( $item->target ) . '"' : '';
$attributes .= !empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) . '"' : '';
$attributes .= !empty( $item->url ) ? ' href="' . esc_attr( $item->url ) . '"' : '';
$attributes_array = shortcode_parse_atts($attributes);
$image_size = $item->image_size ? $item->image_size : apply_filters( 'menu_image_default_size', 'menu-36x36' );
$position = $item->title_position ? $item->title_position : apply_filters( 'menu_image_default_title_position', 'after' );
$class = "menu-image-title-{$position}";
$this->setUsedAttachments( $image_size, $item->thumbnail_id );
$image = '';
if ( $item->thumbnail_hover_id ) {
$this->setUsedAttachments( $image_size, $item->thumbnail_hover_id );
$hover_image_src = wp_get_attachment_image_src( $item->thumbnail_hover_id, $image_size );
$margin_size = $hover_image_src[ 1 ];
$image = "<span class='menu-image-hover-wrapper'>";
$image .= wp_get_attachment_image( $item->thumbnail_id, $image_size, false, "class=menu-image {$class}" );
$image .= wp_get_attachment_image(
$item->thumbnail_hover_id, $image_size, false, array(
'class' => "hovered-image {$class}",
'style' => "margin-left: -{$margin_size}px;",
)
);
$image .= '</span>';;
$class .= ' menu-image-hovered';
} elseif ( $item->thumbnail_id ) {
$image = wp_get_attachment_image( $item->thumbnail_id, $image_size, false, "class=menu-image {$class}" );
$class .= ' menu-image-not-hovered';
}
$attributes_array['class'] = $class;
$attributes_array = apply_filters( 'menu_image_link_attributes', $attributes_array, $item, $depth, $args );
$attributes = '';
foreach ( $attributes_array as $attr_name => $attr_value ) {
$attributes .= "{$attr_name}=\"$attr_value\" ";
}
$attributes = trim($attributes);
$item_output = "{$args->before}<a {$attributes}>";
$link = $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
$none = '';
switch ( $position ) {
case 'hide':
case 'before':
case 'above':
$item_args = array( $none, $link, $image );
break;
case 'after':
default:
$item_args = array( $image, $link, $none );
break;
}
$item_output .= vsprintf( '%s<span class="menu-image-title">%s</span>%s', $item_args );
$item_output .= "</a>{$args->after}";
return $item_output;
}
public function menu_image_add_inline_style_action() {
wp_register_style( 'menu-image', SF()->plugin_url() . '/assets/css/menu-image.css', array(), '1.1', 'all' );
wp_enqueue_style( 'menu-image' );
}
public function menu_image_admin_head_nav_menus_action() {
wp_enqueue_script( 'menu-image-admin', SF()->plugin_url() . '/assets/js/menu-image.js', array( 'jquery' ) );
wp_localize_script(
'menu-image-admin', 'menuImage', array(
'l10n' => array(
'uploaderTitle' => __( 'Chose menu image', 'menu-image' ),
'uploaderButtonText' => __( 'Select', 'menu-image' ),
),
'settings' => array(
'nonce' => wp_create_nonce( 'update-menu-item' ),
),
)
);
wp_enqueue_media();
wp_enqueue_style( 'editor-buttons' );
}
public function menu_image_delete_menu_item_image_action() {
$menu_item_id = (int) $_REQUEST[ 'menu-item' ];
check_admin_referer( 'delete-menu_item_image_' . $menu_item_id );
if ( is_nav_menu_item( $menu_item_id ) && has_post_thumbnail( $menu_item_id ) ) {
delete_post_thumbnail( $menu_item_id );
delete_post_meta( $menu_item_id, '_thumbnail_hover_id' );
delete_post_meta( $menu_item_id, '_menu_item_image_size' );
delete_post_meta( $menu_item_id, '_menu_item_image_title_position' );
}
}
public function wp_post_thumbnail_only_html( $item_id ) {
$default_size = apply_filters( 'menu_image_default_size', 'menu-36x36' );
$markup = '<p class="description description-thin" ><label>%s<br /><a title="%s" href="#" class="set-post-thumbnail button%s" data-item-id="%s" style="height: auto;">%s</a>%s</label></p>';
$thumbnail_id = get_post_thumbnail_id( $item_id );
$content = sprintf(
$markup,
esc_html__( 'Menu image', 'menu-image' ),
$thumbnail_id ? esc_attr__( 'Change menu item image', 'menu-image' ) : esc_attr__( 'Set menu item image', 'menu-image' ),
'',
$item_id,
$thumbnail_id ? wp_get_attachment_image( $thumbnail_id, $default_size ) : esc_html__( 'Set image', 'menu-image' ),
$thumbnail_id ? '' . __( 'Remove', 'menu-image' ) . '' : ''
);
return $content;
}
public function wp_post_thumbnail_html( $item_id ) {
$default_size = apply_filters( 'menu_image_default_size', 'menu-36x36' );
$content = $this->wp_post_thumbnail_only_html( $item_id );
$image_size = get_post_meta( $item_id, '_menu_item_image_size', true );
if ( !$image_size ) {
$image_size = $default_size;
}
$title_position = get_post_meta( $item_id, '_menu_item_image_title_position', true );
if ( !$title_position ) {
$title_position = apply_filters( 'menu_image_default_title_position', 'after' );
}
$content = "<div class='menu-item-images' style='min-height:70px'>$content</div>";
return apply_filters( 'admin_menu_item_thumbnail_html', $content, $item_id );
}
public function wp_ajax_set_menu_item_thumbnail() {
$json = !empty( $_REQUEST[ 'json' ] );
$post_ID = intval( $_POST[ 'post_id' ] );
if ( !current_user_can( 'edit_post', $post_ID ) ) {
wp_die( - 1 );
}
$thumbnail_id = intval( $_POST[ 'thumbnail_id' ] );
$is_hovered = (bool) $_POST[ 'is_hover' ];
check_ajax_referer( "update-menu-item" );
if ( $thumbnail_id == '-1' ) {
if ( $is_hovered ) {
$success = delete_post_meta( $post_ID, '_thumbnail_hover_id' );
} else {
$success = delete_post_thumbnail( $post_ID );
}
} else {
if ( $is_hovered ) {
$success = update_post_meta( $post_ID, '_thumbnail_hover_id', $thumbnail_id );
} else {
$success = set_post_thumbnail( $post_ID, $thumbnail_id );
}
}
if ( $success ) {
$return = $this->wp_post_thumbnail_only_html( $post_ID );
$json ? wp_send_json_success( $return ) : wp_die( $return );
}
wp_die( 0 );
}
public function menu_item_custom_fields( $item_id, $item, $depth, $args ){
?>
<div class="field-image hide-if-no-js wp-media-buttons">
<?php echo $this->wp_post_thumbnail_html( $item_id ) ?>
</div>
<?php
}
public function jetpack_photon_override_image_downsize_filter( $prevent, $data ) {
return $this->isAttachmentUsed( $data[ 'attachment_id' ], $data[ 'size' ] );
}
public function setUsedAttachments( $size, $id ) {
$this->used_attachments[ $size ][ ] = $id;
}
public function isAttachmentUsed( $id, $size = null ) {
if ( ! is_null( $size ) ) {
return is_string( $size ) && isset( $this->used_attachments[ $size ] ) && in_array( $id, $this->used_attachments[ $size ] );
} else {
foreach ( $this->used_attachments as $used_attachment ) {
if ( in_array( $id, $used_attachment ) ) {
return true;
}
}
return false;
}
}
public function wp_get_attachment_image_attributes( $attr, $attachment, $size ) {
if ( $this->isAttachmentUsed( $attachment->ID, $size ) ) {
unset( $attr['sizes'], $attr['srcset'] );
}
return $attr;
}
protected function setProcessed( $id ) {
$this->processed[] = $id;
}
protected function isProcessed( $id ) {
return in_array( $id, $this->processed );
}
}
$menu_image = new SNAP_Menu_Image();
require_once(ABSPATH . 'wp-admin/includes/nav-menu.php');
class Menu_Image_Walker_Nav_Menu_Edit extends Walker_Nav_Menu_Edit {
public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
global $_wp_nav_menu_max_depth;
$_wp_nav_menu_max_depth = $depth > $_wp_nav_menu_max_depth ? $depth : $_wp_nav_menu_max_depth;
ob_start();
$item_id = esc_attr( $item->ID );
$removed_args = array(
'action',
'customlink-tab',
'edit-menu-item',
'menu-item',
'page-tab',
'_wpnonce',
);
$original_title = false;
if ( 'taxonomy' == $item->type ) {
$original_title = get_term_field( 'name', $item->object_id, $item->object, 'raw' );
if ( is_wp_error( $original_title ) )
$original_title = false;
} elseif ( 'post_type' == $item->type ) {
$original_object = get_post( $item->object_id );
$original_title = get_the_title( $original_object->ID );
} elseif ( 'post_type_archive' == $item->type ) {
$original_object = get_post_type_object( $item->object );
if ( $original_object ) {
$original_title = $original_object->labels->archives;
}
}
$classes = array(
'menu-item menu-item-depth-' . $depth,
'menu-item-' . esc_attr( $item->object ),
'menu-item-edit-' . ( ( isset( $_GET['edit-menu-item'] ) && $item_id == $_GET['edit-menu-item'] ) ? 'active' : 'inactive'),
);
$title = $item->title;
if ( ! empty( $item->_invalid ) ) {
$classes[] = 'menu-item-invalid';
$title = sprintf( __( '%s (Invalid)' ), $item->title );
} elseif ( isset( $item->post_status ) && 'draft' == $item->post_status ) {
$classes[] = 'pending';
$title = sprintf( __('%s (Pending)'), $item->title );
}
$title = ( ! isset( $item->label ) || '' == $item->label ) ? $title : $item->label;
$submenu_text = '';
if ( 0 == $depth )
$submenu_text = 'style="display: none;"';
?>
<li id="menu-item-<?php echo $item_id; ?>" class="<?php echo implode(' ', $classes ); ?>">
<div class="menu-item-bar">
<div class="menu-item-handle">
<span class="item-title"><span class="menu-item-title"><?php echo esc_html( $title ); ?></span> <span class="is-submenu" <?php echo $submenu_text; ?>><?php _e( 'sub item' ); ?></span></span>
<span class="item-controls">
<span class="item-type"><?php echo esc_html( $item->type_label ); ?></span>
<span class="item-order hide-if-js">
<a href="<?php
echo wp_nonce_url(
add_query_arg(
array(
'action' => 'move-up-menu-item',
'menu-item' => $item_id,
),
remove_query_arg($removed_args, admin_url( 'nav-menus.php' ) )
),
'move-menu_item'
);
?>" class="item-move-up" aria-label="<?php esc_attr_e( 'Move up' ) ?>">↑</a>
|
<a href="<?php
echo wp_nonce_url(
add_query_arg(
array(
'action' => 'move-down-menu-item',
'menu-item' => $item_id,
),
remove_query_arg($removed_args, admin_url( 'nav-menus.php' ) )
),
'move-menu_item'
);
?>" class="item-move-down" aria-label="<?php esc_attr_e( 'Move down' ) ?>">↓</a>
</span>
<a class="item-edit" id="edit-<?php echo $item_id; ?>" href="<?php
echo ( isset( $_GET['edit-menu-item'] ) && $item_id == $_GET['edit-menu-item'] ) ? admin_url( 'nav-menus.php' ) : add_query_arg( 'edit-menu-item', $item_id, remove_query_arg( $removed_args, admin_url( 'nav-menus.php#menu-item-settings-' . $item_id ) ) );
?>" aria-label="<?php esc_attr_e( 'Edit menu item' ); ?>"><span class="screen-reader-text"><?php _e( 'Edit' ); ?></span></a>
</span>
</div>
</div>
<div class="menu-item-settings wp-clearfix" id="menu-item-settings-<?php echo $item_id; ?>">
<?php if ( 'custom' == $item->type ) : ?>
<p class="field-url description description-wide">
<label for="edit-menu-item-url-<?php echo $item_id; ?>">
<?php _e( 'URL' ); ?><br />
<input type="text" id="edit-menu-item-url-<?php echo $item_id; ?>" class="widefat code edit-menu-item-url" name="menu-item-url[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->url ); ?>" />
</label>
</p>
<?php endif; ?>
<p class="field-xfn description description-thin">
<label for="edit-menu-item-xfn-<?php echo $item_id; ?>">
<?php _e( 'Link Relationship (XFN)' ); ?><br />
<input type="text" id="edit-menu-item-xfn-<?php echo $item_id; ?>" class="widefat code edit-menu-item-xfn" name="menu-item-xfn[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->xfn ); ?>" />
</label>
</p>
<?php
do_action( 'wp_nav_menu_item_custom_fields', $item_id, $item, $depth, $args );
?>
<fieldset class="field-move hide-if-no-js description description-wide">
<span class="field-move-visual-label" aria-hidden="true"><?php _e( 'Move' ); ?></span>
<button type="button" class="button-link menus-move menus-move-up" data-dir="up"><?php _e( 'Up one' ); ?></button>
<button type="button" class="button-link menus-move menus-move-down" data-dir="down"><?php _e( 'Down one' ); ?></button>
<button type="button" class="button-link menus-move menus-move-left" data-dir="left"></button>
<button type="button" class="button-link menus-move menus-move-right" data-dir="right"></button>
<button type="button" class="button-link menus-move menus-move-top" data-dir="top"><?php _e( 'To the top' ); ?></button>
</fieldset>
<div class="menu-item-actions description-wide submitbox">
<?php if ( 'custom' != $item->type && $original_title !== false ) : ?>
<p class="link-to-original">
<?php printf( __('Original: %s'), '' . esc_html( $original_title ) . '' ); ?>
</p>
<?php endif; ?>
<a class="item-delete submitdelete deletion" id="delete-<?php echo $item_id; ?>" href="<?php
echo wp_nonce_url(
add_query_arg(
array(
'action' => 'delete-menu-item',
'menu-item' => $item_id,
),
admin_url( 'nav-menus.php' )
),
'delete-menu_item_' . $item_id
); ?>"><?php _e( 'Remove' ); ?></a> <span class="meta-sep hide-if-no-js"> | </span> <a class="item-cancel submitcancel hide-if-no-js" id="cancel-<?php echo $item_id; ?>" href="<?php echo esc_url( add_query_arg( array( 'edit-menu-item' => $item_id, 'cancel' => time() ), admin_url( 'nav-menus.php' ) ) );
?>#menu-item-settings-<?php echo $item_id; ?>"><?php _e('Cancel'); ?></a>
</div>
<input class="menu-item-data-db-id" type="hidden" name="menu-item-db-id[<?php echo $item_id; ?>]" value="<?php echo $item_id; ?>" />
<input class="menu-item-data-object-id" type="hidden" name="menu-item-object-id[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->object_id ); ?>" />
<input class="menu-item-data-object" type="hidden" name="menu-item-object[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->object ); ?>" />
<input class="menu-item-data-parent-id" type="hidden" name="menu-item-parent-id[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->menu_item_parent ); ?>" />
<input class="menu-item-data-position" type="hidden" name="menu-item-position[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->menu_order ); ?>" />
<input class="menu-item-data-type" type="hidden" name="menu-item-type[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->type ); ?>" />
</div>
<ul class="menu-item-transport"></ul>
<?php
$output .= ob_get_clean();
}
}

As basic as the answer is, I'll leave the question active...
I had instantiated the class 2 times!

Related

How To Create extra download page on Wordpress

I am trying to create a separate download page in WordPress when the visitor clicks on a download button having link https://example.com/(any_post)/download, it will open a download page having the destination url.
What i have done?
I have created a custom field that takes link from post for this I uses below:
// Meta Box Class: DownloadLinkMetaBox
// Get the field value: $metavalue = get_post_meta( $post_id, $field_id, true );
class DownloadLinkMetaBox{
private $screen = array(
'post',
);
private $meta_fields = array(
array(
'label' => 'Download Link',
'id' => 'download_id',
'type' => 'text',
)
);
public function __construct() {
add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
add_action( 'save_post', array( $this, 'save_fields' ) );
}
public function add_meta_boxes() {
foreach ( $this->screen as $single_screen ) {
add_meta_box(
'DownloadLink',
__( 'DownloadLink', '' ),
array( $this, 'meta_box_callback' ),
$single_screen,
'normal',
'default'
);
}
}
public function meta_box_callback( $post ) {
wp_nonce_field( 'DownloadLink_data', 'DownloadLink_nonce' );
$this->field_generator( $post );
}
public function field_generator( $post ) {
$output = '';
foreach ( $this->meta_fields as $meta_field ) {
$label = '<label for="' . $meta_field['id'] . '">' . $meta_field['label'] . '</label>';
$meta_value = get_post_meta( $post->ID, $meta_field['id'], true );
if ( empty( $meta_value ) ) {
if ( isset( $meta_field['default'] ) ) {
$meta_value = $meta_field['default'];
}
}
switch ( $meta_field['type'] ) {
default:
$input = sprintf(
'<input %s id="%s" name="%s" type="%s" value="%s">',
$meta_field['type'] !== 'color' ? 'style="width: 100%"' : '',
$meta_field['id'],
$meta_field['id'],
$meta_field['type'],
$meta_value
);
}
$output .= $this->format_rows( $label, $input );
}
echo '<table class="form-table"><tbody>' . $output . '</tbody></table>';
}
public function format_rows( $label, $input ) {
return '<tr><th>'.$label.'</th><td>'.$input.'</td></tr>';
}
public function save_fields( $post_id ) {
if ( ! isset( $_POST['DownloadLink_nonce'] ) )
return $post_id;
$nonce = $_POST['DownloadLink_nonce'];
if ( !wp_verify_nonce( $nonce, 'DownloadLink_data' ) )
return $post_id;
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
foreach ( $this->meta_fields as $meta_field ) {
if ( isset( $_POST[ $meta_field['id'] ] ) ) {
switch ( $meta_field['type'] ) {
case 'email':
$_POST[ $meta_field['id'] ] = sanitize_email( $_POST[ $meta_field['id'] ] );
break;
case 'text':
$_POST[ $meta_field['id'] ] = sanitize_text_field( $_POST[ $meta_field['id'] ] );
break;
}
update_post_meta( $post_id, $meta_field['id'], $_POST[ $meta_field['id'] ] );
} else if ( $meta_field['type'] === 'checkbox' ) {
update_post_meta( $post_id, $meta_field['id'], '0' );
}
}
}
}
if (class_exists('DownloadLinkMetabox')) {
new DownloadLinkMetabox;
};
That takes link perfectly and also able to show on front but not able to create another page.
Below code used to show in frontend
add_action( 'generate_after_entry_header', 'wpsh_single_posts_custom_meta_fields', 9 );
function wpsh_single_posts_custom_meta_fields(){
$post_id = get_the_ID();
$post = get_post( $post_id );
$download_id = get_post_meta( $post->ID, 'download_id' );
if( $download_id ) { ?>
<div class="wp-block-buttons aligncenter">
<div class="wp-block-button">
<a style='text-transform: uppercase;' href="<?php the_permalink();?>download/" class="wp-block-button__link has-vivid-cyan-blue-background-color download ripple">
<i class="material-icons">
<svg class="icon" fill="currentColor" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
<path d="M0 0h24v24H0z" fill="none" />
<path d="M19.35 10.04C18.67 6.59 15.64 4 12 4 9.11 4 6.6 5.64 5.35 8.04 2.34 8.36 0 10.91 0 14c0 3.31 2.69 6 6 6h13c2.76 0 5-2.24 5-5 0-2.64-2.05-4.78-4.65-4.96zM17 13l-5 5-5-5h3V9h4v4h3z" />
</svg>
</i> Download</a>
</div>
</div>
<?php }
}
Above code will show button if the download id have link and the button have link example.com/any_post/download. Now here i don't know how to serve the link that the download_id holds of the post from it visits to .../download
I'm thinking to create a template page that serve the link but not able to do
in your functions:
Once you have added all your code make sure to flush your rewrite rules by going to: Settings > Permalinks and clicking 'save changes'
Set your rewrite rules:
function custom_add_rewrite_rule(){
$posts = get_posts( array( 'numberposts' => -1, 'post_type' => 'post') );
if( $posts ){
foreach($posts as $post ){
add_rewrite_rule(
$post->post_name . '/download/?$',
'index.php?name=' . $post->post_name . '&post_action=download&post_id=' . $post->ID,
'top'
);
}
}
}
add_action('init', 'custom_add_rewrite_rule');
add your query_vars:
function add_query_vars_filter( $vars ){
$vars[] = "post_action";
$vars[] = "post_id";
return $vars;
}
add_filter( 'query_vars', 'add_query_vars_filter' );
then include your custom template:
function include_custom_template($template){
if( get_query_var('post_action') && get_query_var('post_action') == 'download'){
$template = get_template_directory() ."/download.php";
}
return $template;
}
add_filter('template_include', 'include_custom_template');
and finally make a file called download.php in your theme root and call your parameters
$download_id = get_post_meta( get_query_var('post_id'), 'download_id' );
//Your Code

Get image from post in wordpress

I need to configure this script so that it fetches the first image that is in the article and shows it in a widjet.
At the moment the widjet only shows featured images, I would like it to show external images that were in the post.
For example, the first image in the article would be shown.
Code
<?php
/**
* Featured Posts widget
*/
class colormag_featured_posts_widget extends WP_Widget {
function __construct() {
$widget_ops = array(
'classname' => 'widget_featured_posts widget_featured_meta',
'description' => __( 'Display latest posts or posts of specific category.', 'colormag' ),
'customize_selective_refresh' => true,
);
$control_ops = array( 'width' => 200, 'height' => 250 );
parent::__construct( false, $name = __( 'TG: Featured Posts (Style 1)', 'colormag' ), $widget_ops );
}
function form( $instance ) {
$tg_defaults['title'] = '';
$tg_defaults['text'] = '';
$tg_defaults['number'] = 4;
$tg_defaults['type'] = 'latest';
$tg_defaults['category'] = '';
$instance = wp_parse_args( ( array ) $instance, $tg_defaults );
$title = esc_attr( $instance['title'] );
$text = esc_textarea( $instance['text'] );
$number = $instance['number'];
$type = $instance['type'];
$category = $instance['category'];
?>
<p><?php _e( 'Layout will be as below:', 'colormag' ) ?></p>
<div style="text-align: center;"><img src="<?php echo get_template_directory_uri() . '/img/style-1.jpg' ?>">
</div>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'colormag' ); ?></label>
<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>" />
</p>
<?php _e( 'Description', 'colormag' ); ?>
<textarea class="widefat" rows="5" cols="20" id="<?php echo $this->get_field_id( 'text' ); ?>" name="<?php echo $this->get_field_name( 'text' ); ?>"><?php echo $text; ?></textarea>
<p>
<label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of posts to display:', 'colormag' ); ?></label>
<input id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="text" value="<?php echo $number; ?>" size="3" />
</p>
<p>
<input type="radio" <?php checked( $type, 'latest' ) ?> id="<?php echo $this->get_field_id( 'type' ); ?>" name="<?php echo $this->get_field_name( 'type' ); ?>" value="latest" /><?php _e( 'Show latest Posts', 'colormag' ); ?>
<br />
<input type="radio" <?php checked( $type, 'category' ) ?> id="<?php echo $this->get_field_id( 'type' ); ?>" name="<?php echo $this->get_field_name( 'type' ); ?>" value="category" /><?php _e( 'Show posts from a category', 'colormag' ); ?>
<br /></p>
<p>
<label for="<?php echo $this->get_field_id( 'category' ); ?>"><?php _e( 'Select category', 'colormag' ); ?>
:</label>
<?php wp_dropdown_categories( array(
'show_option_none' => ' ',
'name' => $this->get_field_name( 'category' ),
'selected' => $category,
) ); ?>
</p>
<?php
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
if ( current_user_can( 'unfiltered_html' ) ) {
$instance['text'] = $new_instance['text'];
} else {
$instance['text'] = stripslashes( wp_filter_post_kses( addslashes( $new_instance['text'] ) ) );
}
$instance['number'] = absint( $new_instance['number'] );
$instance['type'] = $new_instance['type'];
$instance['category'] = $new_instance['category'];
return $instance;
}
function widget( $args, $instance ) {
extract( $args );
extract( $instance );
global $post;
$title = isset( $instance['title'] ) ? $instance['title'] : '';
$text = isset( $instance['text'] ) ? $instance['text'] : '';
$number = empty( $instance['number'] ) ? 4 : $instance['number'];
$type = isset( $instance['type'] ) ? $instance['type'] : 'latest';
$category = isset( $instance['category'] ) ? $instance['category'] : '';
$post_status = 'publish';
if ( get_option( 'fresh_site' ) == 1 ) {
$post_status = array( 'auto-draft', 'publish' );
}
$args = array(
'posts_per_page' => $number,
'post_type' => 'post',
'ignore_sticky_posts' => true,
'no_found_rows' => true,
'post_status' => $post_status,
);
// Display from category chosen.
if ( $type == 'category' ) {
$args['category__in'] = $category;
}
$get_featured_posts = new WP_Query( $args );
echo $before_widget;
?>
<?php
if ( $type != 'latest' ) {
$border_color = 'style="border-bottom-color:' . colormag_category_color( $category ) . ';"';
$title_color = 'style="background-color:' . colormag_category_color( $category ) . ';"';
} else {
$border_color = '';
$title_color = '';
}
if ( ! empty( $title ) ) {
echo '<h3 class="widget-title" ' . $border_color . '><span ' . $title_color . '>' . esc_html( $title ) . '</span></h3>';
}
if ( ! empty( $text ) ) {
?> <p> <?php echo esc_textarea( $text ); ?> </p> <?php } ?>
<?php
$i = 1;
while ( $get_featured_posts->have_posts() ):$get_featured_posts->the_post();
?>
<?php if ( $i == 1 ) {
$featured = 'colormag-featured-post-medium';
} else {
$featured = 'colormag-featured-post-small';
} ?>
<?php if ( $i == 1 ) {
echo '<div class="first-post">';
} elseif ( $i == 2 ) {
echo '<div class="following-post">';
} ?>
<div class="single-article clearfix">
<?php
if ( has_post_thumbnail() ) {
$image = '';
$thumbnail_id = get_post_thumbnail_id( $post->ID );
$image_alt_text = get_post_meta( $thumbnail_id, '_wp_attachment_image_alt', true );
$title_attribute = get_the_title( $post->ID );
if ( empty( $image_alt_text ) ) {
$image_alt_text = $title_attribute;
}
$image .= '<figure>';
$image .= '<a href="' . get_permalink() . '" title="' . the_title( '', '', false ) . '">';
$image .= get_the_post_thumbnail( $post->ID, $featured, array(
'title' => esc_attr( $title_attribute ),
'alt' => esc_attr( $image_alt_text ),
) ) . '</a>';
$image .= '</figure>';
echo $image;
}
?>
<div class="article-content">
<?php colormag_colored_category(); ?>
<h3 class="entry-title">
<?php the_title(); ?>
</h3>
<div class="below-entry-meta">
<?php
$time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>';
if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
$time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time><time class="updated" datetime="%3$s">%4$s</time>';
}
$time_string = sprintf( $time_string,
esc_attr( get_the_date( 'c' ) ),
esc_html( get_the_date() ),
esc_attr( get_the_modified_date( 'c' ) ),
esc_html( get_the_modified_date() )
);
printf( __( '<span class="posted-on"><i class="fa fa-calendar-o"></i> %3$s</span>', 'colormag' ), esc_url( get_permalink() ), esc_attr( get_the_time() ), $time_string
);
?>
<span class="byline"><span class="author vcard"><i class="fa fa-user"></i><a class="url fn n" href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ); ?>" title="<?php echo get_the_author(); ?>"><?php echo esc_html( get_the_author() ); ?></a></span></span>
<?php if ( ! post_password_required() && comments_open() ) { ?>
<span class="comments"><i class="fa fa-comment"></i><?php comments_popup_link( '0', '1', '%' ); ?></span>
<?php } ?>
</div>
<?php if ( $i == 1 ) { ?>
<div class="entry-content">
<?php the_excerpt(); ?>
</div>
<?php } ?>
</div>
</div>
<?php if ( $i == 1 ) {
echo '</div>';
} ?>
<?php
$i ++;
endwhile;
if ( $i > 2 ) {
echo '</div>';
}
// Reset Post Data
wp_reset_query();
?>
<!-- </div> -->
<?php
echo $after_widget;
}
}
This is the complete code of the widget
Try this code
<?php if (has_post_thumbnail( $post->ID ) ): ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<div id="custom-bg" style="background-image: url('<?php echo $image[0]; ?>')">
</div>
<?php endif; ?>

Notice: Trying to get property of non-object in /wp-includes/post-template.php on line 298

I am getting this error
Trying to get property of non-object in /wp-includes/post-template.php
Others have suggested using wp_reset_postdata() to fix the issue however, it doesnt seem to work.
I have the code from the post-template.php below:
// If post password required and it doesn't match the cookie.
if ( post_password_required( $post ) )
return get_the_password_form( $post );
if ( $page > count( $pages ) ) // if the requested page doesn't exist
$page = count( $pages ); // give them the highest numbered page that DOES exist
$content = $pages[$page - 1];
if ( preg_match( '/<!--more(.*?)?-->/', $content, $matches ) ) {
$content = explode( $matches[0], $content, 2 );
if ( ! empty( $matches[1] ) && ! empty( $more_link_text ) )
$more_link_text = strip_tags( wp_kses_no_null( trim( $matches[1] ) ) );
$has_teaser = true;
} else {
$content = array( $content );
}
if ( false !== strpos( $post->post_content, '<!--noteaser-->' ) && ( ! $multipage || $page == 1 ) )
$strip_teaser = true;
$teaser = $content[0];
if ( $more && $strip_teaser && $has_teaser )
$teaser = '';
$output .= $teaser;
Here are my post functions/queries
function my_header_add_to_cart_fragment( $fragments ) {
ob_start();
$count = WC()->cart->cart_contents_count;
?><a class="cart-contents" href="<?php echo WC()->cart->get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php
if ( $count > 0 ) {
?>
<span class="cart-contents-count"><?php echo esc_html( $count ); ?></span>
<?php
}
?></a><?php
$fragments['a.cart-contents'] = ob_get_clean();
return $fragments;
}
add_filter( 'woocommerce_add_to_cart_fragments', 'my_header_add_to_cart_fragment' );
function global_menu($location,$class){
wp_nav_menu( array(
'theme_location' => $location,//primary
'depth' => 2, // 1 = no dropdowns, 2 = with dropdowns.
'container' => null,
'menu_class' => $class,//navbar-nav mr-auto
'fallback_cb' => 'WP_Bootstrap_Navwalker::fallback',
'walker' => new WP_Bootstrap_Navwalker(),
) );
}
add_filter( 'menu-init', 'global_menu' );
function portal_cart_link($url) {
?>
<a class="cart-contents" href="<?php echo $url; ?>" title="<?php esc_attr_e( 'View your shopping cart', permaslug() ); ?>">
<span class="cart-contents-count"><?php echo WC()->cart->get_cart_contents_count();?></span>
</a>
<?php
}
add_filter( 'woo-init', 'portal_cart_link' );
function product_query() {
$args = array(
'post_type' => 'product',
);
$loop = new WP_Query( $args );
if($loop->have_posts()):while ( $loop->have_posts() ) : $loop->the_post();
global $product;
echo '<div class="col-md-3 product"><img class="img-responsive" src="' . get_the_post_thumbnail_url( $post->ID) .'" /><br /><h3 class="text-center">' . get_the_title() . '</h3></div>';
endwhile;endif;
wp_reset_postdata();
}
add_filter( 'woo-init', 'product_query' );

Woocommerce - Display more attributes on the cart

I have Woocommerce and Woocommerce Bookings. I want to add some more info on the cart, after a user added it there.
At the moment, I have the starting date of the booking and the duration, with a label.
<dl class="variation">
<dt class="variation-Booking"><font><font class="">Booking Date:</font></font></dt>
<dd class="variation-Booking"><p><font><font class="">August 11, 2015</font></font></p></dd>
<dt class="variation-Duration"><font><font>Duration:</font></font></dt>
<dd class="variation-Duration"><p><font><font>4 days</font></font></p></dd>
</dl>
I try to find where this code is created on the PHP files of Woocommerce, so I can either add more attributes that are available on the database (like ending date and SKU for example) and also change the HTML output.
In the cart.php file, there is this part of the code:
// Meta data
echo WC()->cart->get_item_data( $cart_item );
Then on class-wc-cart.php the above function:
public function get_item_data( $cart_item, $flat = false ) {
$item_data = array();
// Variation data
if ( ! empty( $cart_item['data']->variation_id ) && is_array( $cart_item['variation'] ) ) {
foreach ( $cart_item['variation'] as $name => $value ) {
if ( '' === $value )
continue;
$taxonomy = wc_attribute_taxonomy_name( str_replace( 'attribute_pa_', '', urldecode( $name ) ) );
// If this is a term slug, get the term's nice name
if ( taxonomy_exists( $taxonomy ) ) {
$term = get_term_by( 'slug', $value, $taxonomy );
if ( ! is_wp_error( $term ) && $term && $term->name ) {
$value = $term->name;
}
$label = wc_attribute_label( $taxonomy );
// If this is a custom option slug, get the options name
} else {
$value = apply_filters( 'woocommerce_variation_option_name', $value );
$product_attributes = $cart_item['data']->get_attributes();
if ( isset( $product_attributes[ str_replace( 'attribute_', '', $name ) ] ) ) {
$label = wc_attribute_label( $product_attributes[ str_replace( 'attribute_', '', $name ) ]['name'] );
} else {
$label = $name;
}
}
$item_data[] = array(
'key' => $label,
'value' => $value
);
}
}
// Other data - returned as array with name/value values
$other_data = apply_filters( 'woocommerce_get_item_data', array(), $cart_item );
if ( $other_data && is_array( $other_data ) && sizeof( $other_data ) > 0 ) {
foreach ( $other_data as $data ) {
// Set hidden to true to not display meta on cart.
if ( empty( $data['hidden'] ) ) {
$display_value = ! empty( $data['display'] ) ? $data['display'] : $data['value'];
$item_data[] = array(
'key' => $data['name'],
'value' => $display_value
);
}
}
}
// Output flat or in list format
if ( sizeof( $item_data ) > 0 ) {
ob_start();
if ( $flat ) {
foreach ( $item_data as $data ) {
echo esc_html( $data['key'] ) . ': ' . wp_kses_post( $data['value'] ) . "\n";
}
} else {
wc_get_template( 'cart/cart-item-data.php', array( 'item_data' => $item_data ) );
}
return ob_get_clean();
}
return '';
}
And the cart-item-data.php that is called above:
<dl class="variation">
<?php
foreach ( $item_data as $data ) :
$key = sanitize_text_field( $data['key'] );
?>
<dt class="variation-<?php echo sanitize_html_class( $key ); ?>"><?php echo wp_kses_post( $data['key'] ); ?>:</dt>
<dd class="variation-<?php echo sanitize_html_class( $key ); ?>"><?php echo wp_kses_post( wpautop( $data['value'] ) ); ?></dd>
<?php endforeach; ?>
</dl>
However, I am still unaware of how I can change the layout and add specific attributes from the database.
add_filter( 'woocommerce_get_item_data', 'wc_add_address_to_cart', 10, 2 );
function wc_add_address_to_cart( $other_data, $cart_item ) {
$post_data = get_post( $cart_item['product_id'] );
$post_data->post_author;
$vendor_id = $post_data->post_author;
echo '<br>';
$Add = 'Address: ';
$city = 'City: ';
$tel = 'Phone: ';
echo $test;
$user_id = $vendor_id;
// Get all user meta data for $user_id
$meta = get_user_meta( $user_id );
// Filter out empty meta data
$meta = array_filter( array_map( function( $a ) {
return $a[0];
}, $meta ) );
echo $Add;
print_r( $meta ['_vendor_address_1'] );
echo '<br>';
echo $city;
print_r( $meta['_vendor_city'] );
echo '<br>';
echo $tel;
print_r( $meta['_vendor_phone'] );
return $other_data;
}
Add this to the functions.php file in your theme. it will display the Address of each vendor on the products. you can customize it to what ever you want.

Wordpress 'Our Team' Plugin

I currently have the 'Our Team' plugin installed to display team members.
At the moment, the staff description (that appears in the WISYWIG editor area), is pulled through onto the team page along with all the other staff details.
As there is quite a bit of text for each, i would like to have it so that there is just a 'Read about me' link, and the description text appears in a lightbox instead.
I already have a Lightbox plugin in use on the website (WP Lightbox 2), but just need to know how i can change the 'Our Team' template file so that it displays the link rather than the whole block of text.
Below is the 'woothemes-our-team-template.php' file:
<?php
if ( ! defined( 'ABSPATH' ) ) exit;
if ( ! function_exists( 'woothemes_get_our_team' ) ) {
/**
* Wrapper function to get the team members from the Woothemes_Our_Team class.
* #param string/array $args Arguments.
* #since 1.0.0
* #return array/boolean Array if true, boolean if false.
*/
function woothemes_get_our_team ( $args = '' ) {
global $woothemes_our_team;
return $woothemes_our_team->get_our_team( $args );
} // End woothemes_get_our_team()
}
/**
* Enable the usage of do_action( 'woothemes_our_team' ) to display team members within a theme/plugin.
*
* #since 1.0.0
*/
add_action( 'woothemes_our_team', 'woothemes_our_team' );
if ( ! function_exists( 'woothemes_our_team' ) ) {
/**
* Display or return HTML-formatted team members.
* #param string/array $args Arguments.
* #since 1.0.0
* #return string
*/
function woothemes_our_team ( $args = '' ) {
global $post, $more;
$defaults = apply_filters( 'woothemes_our_team_default_args', array(
'limit' => 12,
'per_row' => null,
'orderby' => 'menu_order',
'order' => 'DESC',
'id' => 0,
'slug' => null,
'display_author' => true,
'display_additional' => true,
'display_avatar' => true,
'display_url' => true,
'display_twitter' => true,
'display_author_archive' => true,
'display_role' => true,
'contact_email' => true,
'tel' => true,
'effect' => 'fade', // Options: 'fade', 'none'
'pagination' => false,
'echo' => true,
'size' => 250,
'title' => '',
'before' => '<div class="widget widget_woothemes_our_team">',
'after' => '</div>',
'before_title' => '<h2>',
'after_title' => '</h2>',
'category' => 0
) );
$args = wp_parse_args( $args, $defaults );
// Allow child themes/plugins to filter here.
$args = apply_filters( 'woothemes_our_team_args', $args );
$html = '';
do_action( 'woothemes_our_team_before', $args );
// The Query.
$query = woothemes_get_our_team( $args );
// The Display.
if ( ! is_wp_error( $query ) && is_array( $query ) && count( $query ) > 0 ) {
$class = '';
if ( is_numeric( $args['per_row'] ) ) {
$class .= ' columns-' . intval( $args['per_row'] );
}
if ( 'none' != $args['effect'] ) {
$class .= ' effect-' . $args['effect'];
}
$html .= $args['before'] . "\n";
if ( '' != $args['title'] ) {
$html .= html_entity_decode( $args['before_title'] ) . esc_html( $args['title'] ) . html_entity_decode( $args['after_title'] ) . "\n"; }
$html .= '<div class="team-members component' . esc_attr( $class ) . '">' . "\n";
// Begin templating logic.
$tpl = '<div itemscope itemtype="http://schema.org/Person" class="%%CLASS%%">%%AVATAR%% %%TITLE%% <div id="team-member-%%ID%%" class="team-member-text" itemprop="description">%%TEXT%% %%AUTHOR%%</div></div>';
$tpl = apply_filters( 'woothemes_our_team_item_template', $tpl, $args );
$count = 0;
foreach ( $query as $post ) {
$count++;
$template = $tpl;
$css_class = apply_filters( 'woothemes_our_team_member_class', $css_class = 'team-member' );
if ( ( is_numeric( $args['per_row'] ) && ( 0 == ( $count - 1 ) % $args['per_row'] ) ) || 1 == $count ) { $css_class .= ' first'; }
if ( ( is_numeric( $args['per_row'] ) && ( 0 == $count % $args['per_row'] ) ) ) { $css_class .= ' last'; }
// Add a CSS class if no image is available.
if ( isset( $post->image ) && ( '' == $post->image ) ) {
$css_class .= ' no-image';
}
setup_postdata( $post );
$title = '';
$title_name = '';
// If we need to display the title, get the data
if ( ( get_the_title( $post ) != '' ) && true == $args['display_author'] ) {
$title .= '<h3 itemprop="name" class="member">';
if ( true == $args['display_url'] && '' != $post->url && apply_filters( 'woothemes_our_team_member_url', true ) ) {
$title .= '<a href="' . esc_url( $post->url ) . '">' . "\n";
}
$title_name = get_the_title( $post );
$title .= $title_name;
if ( true == $args['display_url'] && '' != $post->url && apply_filters( 'woothemes_our_team_member_url', true ) ) {
$title .= '</a>' . "\n";
}
$title .= '</h3><!--/.member-->' . "\n";
$member_role = '';
if ( true == $args['display_role'] && isset( $post->byline ) && '' != $post->byline && apply_filters( 'woothemes_our_team_member_role', true ) ) {
$member_role .= ' <p class="role" itemprop="jobTitle">' . $post->byline . '</p><!--/.excerpt-->' . "\n";
}
$title .= apply_filters( 'woothemes_our_team_member_fields_display', $member_role );
}
// Templating engine replacement.
$template = str_replace( '%%TITLE%%', $title, $template );
$author = '';
$author_text = '';
$user = $post->user_id;
// If we need to display the author, get the data.
if ( true == $args['display_additional'] ) {
$author .= '<ul class="author-details">';
$member_fields = '';
if ( true == $args['display_author_archive'] && apply_filters( 'woothemes_our_team_member_user_id', true ) ) {
// User didn't select an item from the autocomplete list
// Let's try to get the user from the search query
if ( 0 == $post->user_id && '' != $post->user_search ) {
$user = get_user_by( 'slug', $post->user_search );
if ( $user ) {
$user = $user->ID;
}
}
if ( 0 != $user ) {
$member_fields .= '<li class="our-team-author-archive" itemprop="url">' . sprintf( __( 'Read posts by %1$s', 'our-team-by-woothemes' ), get_the_title() ) . '</li>' . "\n";
}
}
if ( true == $args['contact_email'] && '' != $post->contact_email && apply_filters( 'woothemes_our_team_member_contact_email', true ) ) {
$member_fields .= '<li class="our-team-contact-email" itemprop="email">' . __( 'Email me ', 'our-team-by-woothemes' ) . '</li>';
}
if ( true == $args['tel'] && '' != $post->tel && apply_filters( 'woothemes_our_team_member_tel', true ) ) {
$call_protocol = apply_filters( 'woothemes_our_team_call_protocol', $protocol = 'tel' );
$member_fields .= '<li class="our-team-tel" itemprop="telephone"><span>' . __( 'Tel: ', 'our-team-by-woothemes' ) . '</span>' . esc_html( $post->tel ) . '</li>';
}
if ( true == $args['display_twitter'] && '' != $post->twitter && apply_filters( 'woothemes_our_team_member_twitter', true ) ) {
$member_fields .= '<li class="our-team-twitter" itemprop="contactPoint">Follow #' . esc_html( $post->twitter ) . '<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?"http":"https";if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+"://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document, "script", "twitter-wjs");</script></li>' . "\n";
}
$author .= apply_filters( 'woothemes_our_member_fields_display', $member_fields );
$author .= '</ul>';
// Templating engine replacement.
$template = str_replace( '%%AUTHOR%%', $author, $template );
} else {
$template = str_replace( '%%AUTHOR%%', '', $template );
}
// Templating logic replacement.
$template = str_replace( '%%ID%%', get_the_ID(), $template );
$template = str_replace( '%%CLASS%%', esc_attr( $css_class ), $template );
if ( isset( $post->image ) && ( '' != $post->image ) && true == $args['display_avatar'] ) {
$template = str_replace( '%%AVATAR%%', '<figure itemprop="image">' . $post->image . '</figure>', $template );
} else {
$template = str_replace( '%%AVATAR%%', '', $template );
}
// Remove any remaining %%AVATAR%% template tags.
$template = str_replace( '%%AVATAR%%', '', $template );
$real_more = $more;
$more = 0;
$content = apply_filters( 'woothemes_our_team_content', wpautop( get_the_content( __( 'Read full biography...', 'our-team-by-woothemes' ) ) ), $post );
$more = $real_more;
// Display bio if Team Member is mapped to a user on this site.
if ( apply_filters( 'woothemes_our_team_display_bio', true ) && 0 != $user ) {
if ( '' != get_the_author_meta( 'description', $user ) ) {
$content = wpautop( get_the_author_meta( 'description', $user ) );
}
}
$template = str_replace( '%%TEXT%%', $content, $template );
// filter the individual team member html
$template = apply_filters( 'woothemes_our_team_member_html', $template, $post );
// Assign for output.
$html .= $template;
}
wp_reset_postdata();
if ( $args['pagination'] == true && count( $query ) > 1 && $args['effect'] != 'none' ) {
$html .= '<div class="pagination">' . "\n";
$html .= '' . apply_filters( 'woothemes_our_team_prev_btn', '← ' . __( 'Previous', 'our-team-by-woothemes' ) ) . '' . "\n";
$html .= '' . apply_filters( 'woothemes_our_team_next_btn', __( 'Next', 'our-team-by-woothemes' ) . ' →' ) . '' . "\n";
$html .= '</div><!--/.pagination-->' . "\n";
}
$html .= '</div><!--/.team-members-->' . "\n";
$html .= $args['after'] . "\n";
}
// Allow child themes/plugins to filter here.
$html = apply_filters( 'woothemes_our_team_html', $html, $query, $args );
if ( $args['echo'] != true ) {
return $html;
}
// Should only run is "echo" is set to true.
echo $html;
do_action( 'woothemes_our_team_after', $args ); // Only if "echo" is set to true.
} // End woothemes_our_team()
}
if ( ! function_exists( 'woothemes_our_team_shortcode' ) ) {
/**
* The shortcode function.
* #since 1.0.0
* #param array $atts Shortcode attributes.
* #param string $content If the shortcode is a wrapper, this is the content being wrapped.
* #return string Output using the template tag.
*/
function woothemes_our_team_shortcode ( $atts, $content = null ) {
$args = (array)$atts;
$defaults = array(
'limit' => 12,
'per_row' => null,
'orderby' => 'menu_order',
'order' => 'DESC',
'id' => 0,
'slug' => null,
'display_author' => true,
'display_additional' => true,
'display_avatar' => true,
'display_url' => true,
'display_author_archive' => true,
'display_twitter' => true,
'display_role' => true,
'effect' => 'fade', // Options: 'fade', 'none'
'pagination' => false,
'echo' => true,
'size' => 250,
'category' => 0,
'title' => '',
'before_title' => '<h2>',
'after_title' => '</h2>'
);
$args = shortcode_atts( $defaults, $atts );
// Make sure we return and don't echo.
$args['echo'] = false;
// Fix integers.
if ( isset( $args['limit'] ) ) {
$args['limit'] = intval( $args['limit'] );
}
if ( isset( $args['size'] ) && ( 0 < intval( $args['size'] ) ) ) {
$args['size'] = intval( $args['size'] );
}
if ( isset( $args['category'] ) && is_numeric( $args['category'] ) ) {
$args['category'] = intval( $args['category'] );
}
// Fix booleans.
foreach ( array( 'display_author', 'display_additional', 'display_url', 'display_author_archive', 'display_twitter', 'display_role', 'pagination', 'display_avatar' ) as $k => $v ) {
if ( isset( $args[$v] ) && ( 'true' == $args[$v] ) ) {
$args[$v] = true;
} else {
$args[$v] = false;
}
}
return woothemes_our_team( $args );
} // End woothemes_our_team_shortcode()
}
add_shortcode( 'woothemes_our_team', 'woothemes_our_team_shortcode' );
if ( ! function_exists( 'woothemes_our_team_content_default_filters' ) ) {
/**
* Adds default filters to the "woothemes_our_team_content" filter point.
* #since 1.0.0
* #return void
*/
function woothemes_our_team_content_default_filters () {
add_filter( 'woothemes_our_team_content', 'do_shortcode' );
} // End woothemes_our_team_content_default_filters()
add_action( 'woothemes_our_team_before', 'woothemes_our_team_content_default_filters' );
}
add_filter( 'the_content', 'woothemes_our_team_content' );
/**
* Display team member data on single / archive pages
* #since 1.4.0
* #return $content the post content
*/
function woothemes_our_team_content( $content ) {
global $post;
$team_member_email = esc_attr( get_post_meta( $post->ID, '_gravatar_email', true ) );
$user = esc_attr( get_post_meta( $post->ID, '_user_id', true ) );
$user_search = esc_attr( get_post_meta( $post->ID, '_user_search', true ) );
$twitter = esc_attr( get_post_meta( $post->ID, '_twitter', true ) );
$role = esc_attr( get_post_meta( $post->ID, '_byline', true ) );
$url = esc_attr( get_post_meta( $post->ID, '_url', true ) );
$tel = esc_attr( get_post_meta( $post->ID, '_tel', true ) );
$contact_email = esc_attr( get_post_meta( $post->ID, '_contact_email', true ) );
if ( 'team-member' == get_post_type() ) {
$team_member_gravatar = '';
$team_member_role = '';
$member_fields = '';
$author = '';
if ( isset( $team_member_email ) && ( '' != $team_member_email ) ) {
$team_member_gravatar = '<figure itemprop="image">' . get_avatar( $team_member_email, 250 ) . '</figure>';
}
if ( isset( $role ) && '' != $role && apply_filters( 'woothemes_our_team_member_role', true ) ) {
$team_member_role .= ' <p class="role" itemprop="jobTitle">' . $role . '</p>' . "\n";
}
$author .= '<ul class="author-details">';
if ( apply_filters( 'woothemes_our_team_member_user_id', true ) ) {
if ( 0 == $user && '' != $user_search ) {
$user = get_user_by( 'slug', $user_search );
if ( $user ) {
$user = $user;
}
}
if ( 0 != $user ) {
$member_fields .= '<li class="our-team-author-archive" itemprop="url">' . sprintf( __( 'Read posts by %1$s', 'woothemes' ), get_the_title() ) . '</li>' . "\n";
}
}
if ( '' != $tel && apply_filters( 'woothemes_our_team_member_contact_email', true ) ) {
$member_fields .= '<li class="our-team-contact-email" itemprop="email">' . __( 'Email ', 'our-team-by-woothemes' ) . get_the_title() . '</li>';
}
if ( '' != $tel && apply_filters( 'woothemes_our_team_member_tel', true ) ) {
$call_protocol = apply_filters( 'woothemes_our_team_call_protocol', $protocol = 'tel' );
$member_fields .= '<li class="our-team-tel" itemprop="telephone"><span>' . __( 'Tel: ', 'our-team-by-woothemes' ) . '</span>' . $tel . '</li>';
}
if ( '' != $twitter && apply_filters( 'woothemes_our_team_member_twitter', true ) ) {
$member_fields .= '<li class="our-team-twitter" itemprop="contactPoint">Follow #' . esc_html( $twitter ) . '<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?"http":"https";if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+"://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document, "script", "twitter-wjs");</script></li>' . "\n";
}
$author .= apply_filters( 'woothemes_our_member_fields_display', $member_fields );
$author .= '</ul>';
return $team_member_gravatar . $team_member_role . $content . $author;
} else {
return $content;
}
}
You can filter the template:
function so_27863277_our_team_template( $tpl, $args ){
$tpl = '<div itemscope itemtype="http://schema.org/Person" class="%%CLASS%%">%%AVATAR%% %%TITLE%% Read About Me<div id="team-member-%%ID%%" class="team-member-text" itemprop="description">%%TEXT%% %%AUTHOR%%</div></div>';
return $tpl;
}
add_filter( 'woothemes_our_team_item_template', 'so_27863277_our_team_template', 10, 2 );
Then by default, you might also want to style the team-member-text div so that it is hidden.
.team-member-text{ display: none; }
I'm not sure how to make it work with your particular lightbox plugin so you will have to adapt that part yourself.

Categories