how to display the taxonomy terms in hierarchies order? - drupal

I have a vocabulary which displays the terms in the hierarchies order like the following.
parent1
child1
child2
parent2
child1
parent3
parent4
child1
child2
child3
This displays in taxonomy manager.
I want to create a page on which I call a function and pass the vid and displays the taxonomy terms of that vocabulary in hierarchies order just like in taxonomy manager.
I used the following code but it displays only the taxonomy terms not in a tree order.
$vid = 26;
$tree = taxonomy_get_tree($vid);
foreach($tree as $term) {
$output = l($term->name, taxonomy_term_path($term));
if ($term->children) {
$output .= theme('illogica_category_tree', $term->children);
}
}
print $output;
Any idea about this?

I use this:
$tree = extranet_get_nested_tree((int)$vid, null, $tid);
With this:
function extranet_get_nested_tree($terms = array(), $max_depth = NULL, $parent = 0, $parents_index = array(), $depth = 0) {
if (is_int($terms)) {
$terms = taxonomy_get_tree($terms);
}
foreach($terms as $term) {
foreach($term->parents as $term_parent) {
if ($term_parent == $parent) {
$return[$term->tid] = $term;
}
else {
$parents_index[$term_parent][$term->tid] = $term;
}
}
}
foreach($return as &$term) {
if (isset($parents_index[$term->tid]) && (is_null($max_depth) || $depth < $max_depth)) {
$term->children = extranet_get_nested_tree($parents_index[$term->tid], $max_depth, $term->tid, $parents_index, $depth + 1);
}
}
return $return;
}
And to print it out:
function extranet_output_nested_tree($tree) {
if (count($tree)) {
$output = '<ul class="nested-taxonomy-tree">';
foreach ($tree as $term) {
$output .= '<li class="taxonomy-term">';
$output .= t($term->name); //, taxonomy_term_path($term));
if ($term->children) {
$output .= extranet_output_nested_tree( $term->children);
}
$output .= '</li>';
}
$output .= '</ul>';
}
return $output;
}
Enjoy!

taxonomy_get_tree() should return the terms in the correct order, but the array is flat rather than nested. See taxonomy_get_tree() for more information and some sample functions to get a nested array.

Here is the solution `
foreach($terms as $term) {
foreach($term->parents as $term_parent) {
if ($term_parent == $parent) {
$return[$term->tid] = $term;
}
else {
$parents_index[$term_parent][$term->tid] = $term;
}
}
}
foreach($return as &$term) {
if (isset($parents_index[$term->tid]) && (is_null($max_depth) || $depth < $max_depth)) {
$term->children = taxonomy_get_nested_tree($parents_index[$term->tid], $max_depth, $term->tid, $parents_index, $depth + 1);
}
}
return $return;
}
function output_taxonomy_nested_tree($tree) {
if (count($tree)) {
$output = '<ul class="nav navbar-nav">';
foreach ($tree as $term) {
$output .= '<li class="taxonomy-term">' . l($term->name, "taxonomy/term/". $term->tid);
if ($term->children) {
$output .= output_taxonomy_nested_tree($term->children);
}
$output .= '</li>';
}
$output .= '</ul>';
}
return $output;
}
$tree= taxonomy_get_nested_tree(2,10);
$output=output_taxonomy_nested_tree($tree);
echo $output;`
If you want to print it in the specific region you needed just assign the output to a variable in preprocessor function and print the variable in the page tpl file.
$output=output_taxonomy_nested_tree($tree);
echo $output;`
to
$variables['output'] .=output_taxonomy_nested_tree($tree);
place the line
<?php print $output;?>
in your page tpl file

Related

Place ads after second paragraph of the posts in a specific category

I want to place ads after second paragraph of the posts located under a specific category. For this I am trying to run the following piece of codes but it is not working. Kindly help me out.
//Insert ads after second paragraph of single post content.
$catarray = get_the_category( $post->ID );
foreach ($catarray as $cat) {
$catid = $cat->term_id;
if ($catid == 124);
add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
$ad_code = '<div>Insert Ad code here</div>';
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $ad_code, 2, $content );
}
}
return $content;
}
// Parent Function that makes the magic happen
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
$closing_p = '</p>';
$paragraphs = explode( $closing_p, $content );
foreach ($paragraphs as $index => $paragraph) {
if ( trim( $paragraph ) ) {
$paragraphs[$index] .= $closing_p;
}
if ( $paragraph_id == $index + 1 ) {
$paragraphs[$index] .= $insertion;
}
}
return implode( '', $paragraphs );
}
you can use this code for it
add_filter('the_content', 'ak_the_content_filter', 10, 1);
function ak_the_content_filter($content)
{
$catarray = get_the_category( $post->ID );
foreach ($catarray as $cat) {
$catid = $cat->term_id;
if ($catid != 124) { return $content; }
else{
$parags = explode('</p>', $content);
$parags[1] .= '<br>'.do_shortcode('[SHORTCODE HERE]');// add whatever you want after first paragraph
$content_new = '';
foreach ($parags as $parag) {
$content_new .= $parag;
}
return $content_new;
}
}
}

Category slug - Custom post type Wordpress

With custom post type, i need to use category slug for my class (css).
<?php
global $post;
$terms = wp_get_post_terms($post->ID, 'typeresources');
if ($terms) {
$output = array();
foreach ($terms as $term) {
if ($term->term_id != 14)
{
$output[] = '<div class="cat-resources">' .$term->name .'</div>';
}
}
echo join( ' ', $output );
};
?>
Would like to add at my "cat-resources class" the categroy slug
How can i do that ?
There you go.
global $post;
$terms = wp_get_post_terms($post->ID, 'typeresources');
if ($terms) {
$output = array();
foreach ($terms as $term) {
if ($term->term_id != 14) {
$output[] = '<div class="cat-resources '.$term->slug.'">' .$term->name .'</div>';
}
}
echo join( ' ', $output );
};

Litsing user and remove with ajax at wordpress

This code perfectly doing listing users, and remove users with short code. But i have an author directory plugin, and i can list my user with additional meta data. So i want to remove user on my author directory plugin. At the bellow, there is my author directory plugins codes, how can i combine this funciton.
add_action( 'wp_head', 'my_action_javascript' );
function my_action_javascript() {
?>
<script type="text/javascript" >
jQuery(document).ready(function() {
jQuery(".delete_user").click(function() {
var current_element_var = jQuery(this);
var data = {
'action': 'delete_user_action',
'user_id': current_element_var.attr('delete-user-id'),
'security': '<?php echo wp_create_nonce( "security-special-string" ) ?>'
};
jQuery.post('<?php echo admin_url( 'admin-ajax.php' ) ?>', data, function(response) {
if (response == 'deleted_successfully') {
current_element_var.hide();
current_element_var.after('<span> User Deleted Successfully </span>');
current_element_var.remove();
}
});
return false;
});
});
</script>
<?php
}
add_action( 'wp_ajax_delete_user_action', 'delete_user_action_callback' );
add_action( 'wp_ajax_nopriv_delete_user_action', 'delete_user_action_callback' );
function delete_user_action_callback() {
check_ajax_referer( 'security-special-string', 'security' );
wp_delete_user( $_POST['user_id'] );
echo 'deleted_successfully';
die();
}
add_shortcode('listuser', 'listsubscriber');
function listsubscriber() {
$blogusers = get_users( array( 'fields' => array('display_name','ID' )) );
echo '<ul>';
foreach ( $blogusers as $user ) {
echo '<li>' . esc_html( $user->display_name ) . '|' . esc_html( $user->ID ) . '<a class="delete_user" delete-user-id="' . $user->ID . '" href="#">Delete User</a></li>';
}
echo '</ul>';
}
Author list plugin codes at here.
require_once("rules/useroles.php");
require_once("admin/admin.php");
function member_directory_loader_scripts()
{
wp_register_style( 'directorycss', plugin_dir_url( __FILE__ ).'asset/js/jdirectory/directory.css');
wp_enqueue_style( 'directorycss' );
wp_register_script( 'directoryjs', plugin_dir_url( __FILE__ ).'asset/js/jdirectory/jquery.directory.js', array('jquery'));
wp_enqueue_script( 'directoryjs' );
}
add_action( 'wp_enqueue_scripts', 'member_directory_loader_scripts' );
function member_directory_shortcode($atts)
{
global $table_prefix,$wpdb,$post;
$return_content = '';
$return_content .= '<div class="member_directory_table">';
$results = get_users();
if ((!(empty($results))) && (is_array($results)) && (count($results) >0))
{
$m_single = array();
foreach ($results as $single)
{
$user_allowed_listed = true;
$memberDirectoryUserRoleSelect = get_option('memberDirectoryUserRoleSelect');
if (empty($memberDirectoryUserRoleSelect))
{
}
else
{
$user_allowed_listed = check_user_role_allowed($single);
if ($user_allowed_listed == false)
{
continue;
}
}
$return_content .= '<div class="tooltips_list">';
$return_content .= '<span class="tooltips_table_items">';
$return_content .= '<div class="tooltips_table">';
$return_content .= '<div class="tooltips_table_title">';
$enabGlossaryIndexPage = get_option("enabGlossaryIndexPage");
$return_content .= $single->display_name;
$return_content .='</div>';
$return_content .= '<div class="billing">';
$return_content .= $single->billing_address_1;
$return_content .='</div>';
***** i want to at there remove user link with ajax ******
$return_content .= '<div class="billing2">';
$return_content .= $single->billing_company;
$return_content .='</div>';
$return_content .= '<div class="billing3">';
$return_content .= $single->ID;
$return_content .='</div>';
$return_content .= '<div class="tooltips_table_content">';
// old $m_content = $single->user_email;
// 1.3.1
$m_content = '';
$m_content_user_email = $single->user_email;
$m_content_user_bio_in_wp = get_the_author_meta('description',$single->ID);
$m_content .= "<div class = 'member_content_user_email'>";
$m_content .= $m_content_user_email;
$m_content .= "</div>";
$m_content .= "<div class = 'member_content_user_description'>";
$m_content .= $m_content_user_bio_in_wp;
$m_content .= "</div>";
$return_content .= $m_content;
$return_content .='</div>';
$return_content .='</div>';
$return_content .='</span>';
$return_content .='</div>';
}
}
$return_content .= '</div>';
return $return_content;
}
add_shortcode( 'member_directory', 'member_directory_shortcode',10 );
function member_directory_load_footer_js()
{
global $post;
?>
<script type="text/javascript">
var inboxs = new Array();
inboxs['hidezeronumberitem'] = "yes";
inboxs['selectors'] = '.tooltips_list > span';
<?php
$glossaryNavItemFontSize = '12px';
$glossarySelectedNavItemFontSize = get_option("glossarySelectedNavItemFontSize");
$glossarySelectedNavItemFontSize = '14px';
$glossaryNavItemFontSize = '12px';
?>
inboxs['navitemdefaultsize'] = '<?php echo $glossaryNavItemFontSize; ?>';
inboxs['navitemselectedsize'] = '<?php echo $glossarySelectedNavItemFontSize; ?>';
<?php
$glossaryNumbersOrNot = 'no';
if ($choseLanguageForGlossary == 'custom')
{
$glossaryLanguageCustomNavLetters = 'a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z';
?>
inboxs['alphabetletters'] = "<?php echo $glossaryLanguageCustomNavLetters; ?>";
<?php
}
?>
inboxs['number'] = "no";
jQuery(document).ready(function () {
jQuery('.member_directory_table').directory(inboxs);
jQuery('.navitem').css('font-size','<?php echo $glossaryNavItemFontSize; ?>');
})
</script>
<?php
}
add_action('wp_footer','member_directory_load_footer_js');
function check_user_role_allowed($checkuser)
{
$memberDirectoryUserRoleSelect = get_option('memberDirectoryUserRoleSelect');
$saved_allowed_user_roles_in_member_directory = get_option('saved_allowed_user_roles_in_member_directory');
if (empty($memberDirectoryUserRoleSelect))
{
return true;
}
else
{
if ('enableMemberDirectoryUserRolesOption' == $memberDirectoryUserRoleSelect)
{
$can_listed = false;
$checking_user_roles = $checkuser->roles;
if (empty($checking_user_roles))
{
return false ;
}
else
{
foreach ($checking_user_roles as $checking_user_role)
{
if (in_array(strtolower($checking_user_role), $saved_allowed_user_roles_in_member_directory) )
{
$can_listed = true;
return true;
}
}
}
}
if ('disableMemberDirectoryUserRolesOption' == $memberDirectoryUserRoleSelect)
{
$can_listed = true;
$checking_user_roles = $checkuser->roles;
if (empty($checking_user_roles))
{
return false ;
}
else
{
foreach ($checking_user_roles as $checking_user_role)
{
if (in_array(strtolower($checking_user_role), $saved_allowed_user_roles_in_member_directory) )
{
$can_listed = false;
return false;
}
}
}
}
return $can_listed;
}
}
like so
Am guessing that $single->ID contains the User ID. If so, in that place where you marked in code, you could add the delete button.
It would be something like this:
$return_content .= '<div class="billing">';
$return_content .= $single->billing_address_1;
$return_content .='</div>';
/***** i want to at there remove user link with ajax ******/
/*--------- delete button ------------*/
$return_content .= '<button class="delete_user" delete-user-id="'. $single->ID .'">Delete</button>';
/*--------- /delete button ------------*/
$return_content .= '<div class="billing2">';
$return_content .= $single->billing_company;
$return_content .='</div>';
You are already listening to the click event of the class delete_user. And inside it, you are expecting the delete-user-id to have the user_id of the User whom you want to delete! Hence we are using both in our button.
One thing I noticed in code is the add_action( 'wp_ajax_nopriv_delete_user_action', 'delete_user_action_callback' ); This will allow non-logged in users to perform this delete operation because of the wp_ajax_nopriv_*
This hook is functionally the same as wp_ajax_{$action}, except the
“nopriv” variant is used for handling AJAX requests from
unauthenticated users, i.e. when is_user_logged_in() returns false.

Remove a function in Plugin

remove_action is not working I have tried to remove acf_woocommerce_add_fields_to_order using following code
remove remove_action( 'acf_woocommerce_add_fields_to_order', array( ACF_Woo_API::get_instance(), 'acf_woocommerce_add_fields_to_order' ) );
But this is not working kindly help.
The function that I would like to remove from the plugin is as following
public function acf_woocommerce_add_fields_to_order() {
$api = ACF_Woo_API::get_instance();
$group_keys = wp_list_pluck($api->get_field_groups(), $api->acf_id_case_sensitive());
foreach ($group_keys as $group_key => $key) {
$fields = $api->get_field_group_fields($key);
foreach ($fields as $field => $value) {
$field_label = $value['label'];
$raw_meta = base64_decode(get_post_meta(get_the_ID(), $value['key'], true));
$meta = unserialize($raw_meta);
if (is_array($meta)) {
//handle repeater, flexible content
if (is_array(reset($meta))) {
echo '<table style="border-collapse: collapse; width: 100%">';
foreach ($meta as $row) {
echo '<tr>';
foreach ($row as $column) {
echo "<td style='border: 1px solid black;'>$column</td>";
}
echo '</tr>';
}
echo '</table>';
} //handle choice and select
else {
echo '<p><strong>' . $field_label . ': </strong>' . implode('; ', $meta) . '</p>';
}
} else {
$meta = stripcslashes($meta);
echo '<p><strong>' . $field_label . ': </strong>' . $meta . '</p>';
}
}
}
}
Was trying
remove_action( 'acf_woocommerce_add_fields_to_order', array( ACF_Woo_API::get_instance(), 'acf_woocommerce_add_fields_to_order' ) );
but unable to do it.
Hoping to learn more. Thanks!

Wordpress Custom Walker: Current Menu Item

I am new to wordpress and trying to build a selfmade menu structure. I found a custom walker and customized it for my needs. The menu works fine, by now as you can see on my site.
It gets loaded inside the header.php via <?php wp_nav_menu(array('walker' => new Custom_Nav_Walker())); ?>. The command is nested inside an ul tag, so the walker only generates the li tags inside.
Now I want to add a class called active to the current parent menu item and it's dropdown child. I searched the web for hours but didn't find anything I can understand…
_
Can anyone help me implement that class into my custom walker?
Thanks in advance!
Jaro
class Custom_Nav_Walker extends Walker_Nav_Menu {
function start_lvl(&$output, $depth = 0, $args = array()) {
$output .= "";
}
function end_lvl(&$output, $depth = 0, $args = array()) {
$output .= "";
}
function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) {
parent::start_el($item_html, $item, $depth, $args);
if ($item->is_dropdown && ($depth === 0)) {
$output .= "";
} elseif ($depth === 0) {
$output .= "<li class=\"navbar-list-item\"><div class=\"w-dropdown\" data-ix=\"dropdown\"><div class=\"w-dropdown-toggle navbar-link\"><h5>".esc_attr($item->title)."</h5></div><nav class=\"w-dropdown-list dropdown-list\">";
} elseif ($depth > 0) {
$output .= "<a class=\"w-dropdown-link dropdown-link\" href=\"".esc_attr($item->url)."\">".esc_attr($item->title)."";
}
}
function end_el(&$output, $item, $depth=0, $args=array()) {
if ($item->is_dropdown && ($depth === 0)) {
$output .= "";
} elseif ($depth === 0) {
$output .= "</nav></div></li>";
} elseif ($depth > 0) {
$output .= "</a>";
}
}
}
I wrote a new custom walker. This one works now. Maybe somebody will eventually have the same problem sometime. So here is the solution:
class Custom_Nav_Walker extends Walker_Nav_Menu {
function start_lvl(&$output, $depth = 0, $args = array()) {
$output .= "";
}
function end_lvl(&$output, $depth = 0, $args = array()) {
$output .= "";
}
function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) {
$class_names = $value = '';
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$classes[] = 'menu-item-' . $item->ID;
$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
parent::start_el($item_html, $item, $depth, $args);
if ($item->is_dropdown && ($depth === 0)) {
$output .= "";
} elseif ($depth === 0) {
$output .= "<li class=\"navbar-list-item\"><div class=\"w-dropdown\" data-ix=\"dropdown\"><div class=\"w-dropdown-toggle navbar-link ".esc_attr($class_names)."\"><h5>".esc_attr($item->title)."</h5></div><nav class=\"w-dropdown-list dropdown-list\">";
} elseif ($depth > 0) {
$output .= "<a class=\"w-dropdown-link dropdown-link ".esc_attr($class_names)."\" href=\"".esc_attr($item->url)."\">".esc_attr($item->title)."";
}
}
function end_el(&$output, $item, $depth=0, $args=array()) {
if ($item->is_dropdown && ($depth === 0)) {
$output .= "";
} elseif ($depth === 0) {
$output .= "</nav></div></li>";
} elseif ($depth > 0) {
$output .= "</a>";
}
}
}
In the function start_el(&$output, $item, $depth = 0, $args = [], $id = 0) you get the $item->current value. Then you can simply code like:
$current = '';
if ($item->current) {
$current = 'menu-underline';
}
$output .= '<a href="' . $item->url . '" class="nav-link ' . $current . '" type="button">';

Resources