#StackBounty: #functions #filters How can I add a class to a nav li depending on URL?

Bounty: 50

I’m trying to add a .current-menu-item class to a li when on a specific url "/portfolio/". I can achieve this with jQuery:

jQuery(document).ready(function( $ ) {
  var loc = window.location.href; // returns the full URL
  if(/portfolio/.test(loc)) {
    $('#menu-item-33').addClass('current-menu-item');
  }
});

However would like to do with PHP. This what I have so far:

function add_class_to_specific_menu_items( $atts, $item, $args ) {
    // check if the item is set to target="_blank"
    if ( $item->url == '/portfolio/' ) {
      // add the desired attributes:
      $atts['class'] = 'current-menu-item';
    }
    return $atts;
}
add_filter( 'nav_menu_link_attributes', 'add_class_to_specific_menu_items', 10, 3 );

Not sure if this is viable and/or if I can have it add the class to a specific li "#menu-item-33


Get this bounty!!!

#StackBounty: #functions #javascript #errors #block-editor Why is my Gutenberg block returning an error after initial save?

Bounty: 50

I created a Gutenberg block but after saving it and going back into the post I receive this error:

This block contains unexpected or invalid content

I couldn’t find any inconsistencies and I had a friend also take a look over to see if there was anything amiss. What am I doing wrong?

registerBlockType('myblog/check-list', {
    //Built-in Attributes
    title: 'Check List',
    description: 'Generate a bulleted list with green checkmarks',
    icon: 'list-view',
    category: 'design',

    //Custom Attributes
    attributes: {
        title: {
            type: 'string',
            source: 'html',
            selector: 'h2'
        },
        text: {
            type: 'string',
            source: 'html',
            selector: 'p'
        },
        titleColor: {
            type: 'string',
            default: '#383838'
        },
        checkListItemOne: {
            type: 'string',
            source: 'html',
            selector: 'span'
        },
        checkListItemTwo: {
            type: 'string',
            source: 'html',
            selector: 'span'
        }
    },

    //Built-in Functions
    edit({attributes, setAttributes}) {
        const{
            title,
            text,
            titleColor,
            checkListItemOne,
            checkListItemTwo,
        } = attributes;

        //Custom Functions
        
        function onChangeTitle(newTitle) {
            setAttributes( { title: newTitle } );
        }

        function onChangeText(newText) {
            setAttributes( { text: newText } );
        }

        function onTitleColorChange(newColor){
            setAttributes( { titleColor: newColor } );
        }

        function onChangeCheckListItemOne(newListItemOne) {
            setAttributes( { checkListItemOne: newListItemOne})
        }

        function onChangeCheckListItemTwo(newListItemTwo) {
            setAttributes( { checkListItemTwo: newListItemTwo})
        }
        

        return ([
            <InspectorControls style={ { marginBottom: '40px' } }>
                <PanelBody title={ 'Headline Color' }>
                    <p><strong>Choose Title Color</strong></p>
                    <ColorPalette 
                        value={titleColor} 
                        onChange={onTitleColorChange} 
                    />
                </PanelBody>
            </InspectorControls>,

            <div class="col-md-5 offset-md-1">
                <div class="green-check-list-container">
                    <RichText 
                        key="editable"
                        tagName="h2"
                        placeholder="Headline for check list" 
                        value= { title }
                        onChange= { onChangeTitle }
                        style= { { color: titleColor } }
                    />
                    <RichText 
                        key="editable"
                        tagName="p"
                        placeholder="Additional context for the list" 
                        value= { text }
                        onChange= { onChangeText }
                    />
                    <ul class="green-check-list-items">
                        <li>
                            <RichText 
                                key="editable"
                                tagName="span"
                                placeholder="List Item" 
                                value= { checkListItemOne }
                                onChange= { onChangeCheckListItemOne }
                            />
                        </li>                        
                        <li>
                            <RichText 
                                key="editable"
                                tagName="span"
                                placeholder="List Item" 
                                value= { checkListItemTwo }
                                onChange= { onChangeCheckListItemTwo }
                            />
                        </li>
                    </ul>
                </div>
            </div>
        ]);
    },

    save({ attributes }) {
        const {
            title,
            text,
            titleColor,
            checkListItemOne,
            checkListItemTwo,
        } = attributes;

        return (
            <div class="col-md-5 offset-md-1">
                <div class="green-check-list-container">
                    <h2 style={ { color: titleColor } }>{title}</h2>
                    <p>{ text }</p>
                    <ul class="green-check-list-items">
                        <li>
                            <span>{ checkListItemOne }</span>
                        </li>
                        <li>
                            <span>{ checkListItemTwo }</span>
                        </li>
                    </ul>
                </div>
            </div>
        );
    }
});


Get this bounty!!!

#StackBounty: #functions #hooks #pagination #twitter-bootstrap Customize "the_posts_pagination" and put list instead div

Bounty: 50

)

I need help with my custom "the_posts_pagination"

I’m looking to get this :

enter image description here

voila what I did :

in the functions.php, I’ve add this code :

function wp_custom_pagination($args = [], $class = 'pagination') {
    if ($GLOBALS['wp_query']->max_num_pages <= 1) return;

    $args = wp_parse_args( $args, [
            'mid_size'                   => 2,
            'prev_next'                  => false,
            'prev_text'                  => __('Older posts', 'textdomain'),
            'next_text'                  => __('Newer posts', 'textdomain'),
            'screen_reader_text' => __('Posts navigation', 'textdomain'),
    ]);

    $links       = paginate_links($args);
    $next_link = get_previous_posts_link($args['next_text']);
    $prev_link = get_next_posts_link($args['prev_text']);
    $template    = apply_filters( 'navigation_markup_template', '
    <div class="col-auto">
        <a href="" class="btn btn-outline-white text-dark">%3$s</a>
    </div>
    <div class="col-auto">
        <nav class="navigation %1$s" role="navigation">
                <h2 class="screen-reader-text">%2$s</h2>
                <ul class="nav-links pagination mb-0 text-dark">
                    <li class="page-item">%4$s</li>
                </ul>
        </nav>
    </div>
    <div class="col-auto">
        <a href="" class="btn btn-outline-white text-dark">%5$s</a>
    </div>', $args, $class);

    echo sprintf($template, $class, $args['screen_reader_text'], $prev_link, $links, $next_link);}

After, I’ve add this code in my home.php

<?php wp_custom_pagination(); ?>

But, the result is really not what I want!

Here is the html structure code that I would need for this wp_custom_pagination :

            <div class="row justify-content-between align-items-center">
              <div class="col-auto">
                <a href="#" class="btn btn-outline-white text-dark">Previous</a>
              </div>
              <div class="col-auto">
                <nav>
                  <ul class="pagination mb-0 text-dark">
                    <li class="page-item active"><a class="page-link" href="#">1</a></li>
                    <li class="page-item"><a class="page-link" href="#">2</a></li>
                    <li class="page-item"><a class="page-link" href="#">3</a></li>
                  </ul>
                </nav>
              </div>
              <div class="col-auto">
                <a href="#" class="btn btn-outline-white text-dark">Next</a>
              </div>
            </div>

If anyone could help me and tell me and show me my mistakes and how to get what I want please.
Thanks a lot


Get this bounty!!!

#StackBounty: #functions #read-more Why does modifying the "read more" link remove the link class?

Bounty: 50

I’m using the following code (found on the WordPress.org site) to modify my (more…) text:

//Modify "read more" text
function modify_read_more_link() {
    return '<a class="more-link" href="' . get_permalink() . '">(Continue…)</a>';
}
add_filter( 'the_content_more_link', 'modify_read_more_link' );

The code works as expected, in the sense that (more…) is properly replaced by (Continue…), and the link works as intended. However, there is a peculiar issue: The class more-link has disappeared, which means styling doesn’t work. I attempted to circumnavigate the issue by adding styling inline (return '<a style="font-size:0.8em;" href="' . get_permalink() . '">(Continue…)</a>';) but that hasn’t worked either.

My Question:
Why has the class more-link disappeared, and how can I restore it?

Note: I add the code above to functions.php; I’m not using a child theme.


Get this bounty!!!

#StackBounty: #bash #functions A function can only be called if its name is the first word of an expression?

Bounty: 50

A function can only be called if its name is the first word of an expression, as is the case with aliases?
Or can it also be the nth word of an expression?
I can’t emulate a use case that answers this question of mine.
I can’t find a guide that answers this question of mine.
Being Italian, I find it difficult to find the right words on the Google search engine.
Can you also mention the source in the answer?
Thank you.


Get this bounty!!!

#StackBounty: #php #posts #wp-query #functions posts_per_page displays only 2 posts instead of 4 posts

Bounty: 50

I have a wordpress/php code as shown below in which I am trying to pull latest 4 channels at Line A in the code below.

function get_latest_channels( $instance_id = false ) {
    echo $instance_id;
    echo "<br>";
    if ( ! $instance_id ) {
        $query_args = array(
                'post_type'      => 'hello-channels',
                'ep_integrate'   => true,
                'posts_per_page' => 4,
        );
    } else {
        $query_args = array(
                'post_type'      => 'hello-channels',
                'ep_integrate'   => true,
                'meta_key'       => 'instance_id',
                'meta_value'     => $instance_id,
                'posts_per_page' => 4,
        );
    }
    $channels = new WP_Query( $query_args );
    if ( $channels->have_posts() ) {
        $rtn = $channels->posts;
    }

    return $rtn;
}

$instance_id = 49;
$latest_channels = HELLOChannelsget_latest_channels($instance_id);
echo "<pre>"; print_r($latest_channels); echo "</pre>". // Line A

The value of $instance_id is 49. On using 49 value, it should pull 4 latest channels.

Problem Statement:

My code is working fine dev/staging server (meaning its pulling latest 4 channels at Line A) but in production server its pulling only 2 channels.

I am wondering what changes I need to make in the php code above so that it pulls latest 4 channels in the production server.

If I comment this line 'posts_per_page' => 4, in the else section of the function get_latest_channels( $instance_id = false ) then Line A is pulling 30 posts in dev server.


Get this bounty!!!

#StackBounty: #php #posts #functions posts_per_page displays only 2 posts instead of 4 posts

Bounty: 50

I have a wordpress/php code as shown below in which I am trying to pull latest 4 channels at Line A in the code below.

function get_latest_channels( $instance_id = false ) {
    echo $instance_id;
    echo "<br>";
    if ( ! $instance_id ) {
        $query_args = array(
                'post_type'      => 'hello-channels',
                'ep_integrate'   => true,
                'posts_per_page' => 4,
        );
    } else {
        $query_args = array(
                'post_type'      => 'hello-channels',
                'ep_integrate'   => true,
                'meta_key'       => 'instance_id',
                'meta_value'     => $instance_id,
                'posts_per_page' => 4,
        );
    }
    $channels = new WP_Query( $query_args );
    if ( $channels->have_posts() ) {
        $rtn = $channels->posts;
    }

    return $rtn;
}

$instance_id = 49;
$latest_channels = HELLOChannelsget_latest_channels($instance_id);
echo "<pre>"; print_r($latest_channels); echo "</pre>". // Line A

The value of $instance_id is 49. On using 49 value, it should pull 4 latest channels.

Problem Statement:

My code is working fine dev/staging server (meaning its pulling latest 4 channels at Line A) but in production server its pulling only 2 channels.

I am wondering what changes I need to make in the php code above so that it pulls latest 4 channels in the production server.

If I comment this line 'posts_per_page' => 4, in the else section of the function get_latest_channels( $instance_id = false ) then Line A is pulling 30 posts in dev server.


Get this bounty!!!

#StackBounty: #functions #users #forms #permissions Add new user : make the fields First Name and Last name required

Bounty: 50

I’m searching how to make First Name and Last name fields required when we add a new user. Right now only username and Email fields are required.

I found a way by adding class="form-required" for the first and last name fields on the file user-new.php.

But I’m looking for a method with adding code on function.php and not touch to the WordPress Core.

Thanks.


Get this bounty!!!

#StackBounty: #posts #functions #rest-api #security #json Disable REST API for a user ROLE

Bounty: 50

I’m looking for a way to disable Rest API for a user role called ‘external_user’ (disable wp-json queries.)

This user role can see right now alot of posts and pages information with when we put wp-json on the URL (users, pages, posts…)

I use actually the plugin DISABLE REST API but it prevent only not logged users to see json informations. i need to do the same thing with external_user role.

If it’s not possible, can I redirect this user role (and only external_user role) to 404 pages if he try to put an URL with wp-json ?

Thanks.


Get this bounty!!!