If You Are Developer I need Your Help

Zero10

BANNED - Failed to resolve a dispute
Joined
Apr 4, 2022
Messages
562
Reaction score
349
i have a problem with my plugin and theme in WordPress.


My plugin uses a custom post type ( glossary ) and the theme doesn't support that.


so I add to theme functions.php this code and it worked well in the plugin post, but the problem this code didn't get everything from the theme like the Suggested articles.


So can u help me, guys :)


this is the code I use


Code:
function add_glossary_post_type_support( $supported_post_types ) {
    if ( ! is_array( $supported_post_types ) ) {
        $supported_post_types = array();
    }
 
    $supported_post_types[] = 'glossary';
    return $supported_post_types;
}
add_filter( 'TieLabs/Settings/default_post_types', 'add_glossary_post_type_support' );
 
Can you please explain better.
this code didn't get everything from the theme like the Suggested articles
What did you mean by this?

Can you send the link with a description of the final end result of what you are trying to achieve?
 
Let me explain clearly if i post any normal post on my site it works great With all the theme features like the article image, author information, suggested articles, and more

But the plugin uses a custom type called glossary, i use this code

Code:
function add_glossary_post_type_support( $supported_post_types ) {

    if ( ! is_array( $supported_post_types ) ) {

        $supported_post_types = array();

    }

 

    $supported_post_types[] = 'glossary';

    return $supported_post_types;

}

add_filter( 'TieLabs/Settings/default_post_types', 'add_glossary_post_type_support' );


and this code makes my plugin get almost everything from the theme if I change the post layout It also affected the plugin post, but I only have one problem, my custom post glossary didn't get Suggested articles Feature from the theme, like what I have on the normal post
 
You can play with ChatGPT and see if it helps



Prompt:

Check this code to see if its correct:function add_glossary_post_type_support( $supported_post_types ) { if ( ! is_array( $supported_post_types ) ) { $supported_post_types = array(); } $supported_post_types[] = 'glossary'; return $supported_post_types; } add_filter( 'TieLabs/Settings/default_post_types', 'add_glossary_post_type_support' );

Response:

It looks like the code is correct. It adds the 'glossary' post type to the array of supported post types.
Here's a brief overview of what the code does:
  1. The function 'add_glossary_post_type_support()' takes in a single parameter, '$supported_post_types', which is an array of post types that are supported by the theme.
  2. The function checks if the '$supported_post_types' variable is an array. If it is not an array, it initializes it as an empty array.
  3. The function adds the 'glossary' post type to the '$supported_post_types' array using the '[]' operator.
  4. The function returns the '$supported_post_types' array.
  5. The function is then hooked to the 'TieLabs/Settings/default_post_types' filter using the 'add_filter()' function, which allows it to modify the default post types supported by the theme.
Overall, it appears that the code is correctly adding the 'glossary' post type to the array of supported post types for the theme.

Prompt:

Improve this code: function add_glossary_post_type_support( $supported_post_types ) { if ( ! is_array( $supported_post_types ) ) { $supported_post_types = array(); } $supported_post_types[] = 'glossary'; return $supported_post_types; } add_filter( 'TieLabs/Settings/default_post_types', 'add_glossary_post_type_support' );

Response:

Here are a few suggestions for improving the code:

  1. Add a comment at the beginning of the function explaining what it does. This will help other developers understand the purpose of the function more easily.
  2. Consider renaming the function to something more descriptive, such as 'add_support_for_glossary_post_type()'. This will make the purpose of the function more clear.
  3. Add a check to ensure that the 'glossary' post type does not already exist in the array before adding it. This will prevent duplicates from being added to the array.
  4. Consider adding a default value for the '$supported_post_types' parameter. This will allow the function to be called without passing any arguments, which can be convenient in some cases.
Here is the improved code with these suggestions implemented:



/**
* Adds support for the 'glossary' post type to the theme.
*
* @Param array $supported_post_types An array of post types that are supported by the theme.
* @Return array The updated array of supported post types.
*/
function add_support_for_glossary_post_type( $supported_post_types = array() ) {

// Only add the 'glossary' post type if it doesn't already exist in the array.
if ( ! in_array( 'glossary', $supported_post_types ) ) {
$supported_post_types[] = 'glossary';
}

return $supported_post_types;
}
add_filter( 'TieLabs/Settings/default_post_types', 'add_support_for_glossary_post_type' );

Prompt:

Improve this code to get everything from the WordPress theme: function add_glossary_post_type_support( $supported_post_types ) { if ( ! is_array( $supported_post_types ) ) { $supported_post_types = array(); } $supported_post_types[] = 'glossary'; return $supported_post_types; } add_filter( 'TieLabs/Settings/default_post_types', 'add_glossary_post_type_support' );

Response:

It looks like the code is already correctly adding support for the 'glossary' post type to the theme.

If you want to get all post types supported by the theme, you can use the 'get_post_types()' function. This function returns an array of all registered post types on the site. You can pass the 'public' argument as 'true' to only get public post types.

Here's an example of how you can use 'get_post_types()' to get all public post types supported by the theme:

$supported_post_types = get_post_types( array( 'public' => true ) );

Keep in mind that this will return all public post types, not just the ones supported by the theme. To get only the post types supported by the theme, you can use the 'TieLabs/Settings/default_post_types' filter.

For example:
$supported_post_types = apply_filters( 'TieLabs/Settings/default_post_types', array() );
This will return an array of post types supported by the theme. You can then use this array in your code as needed.
 
i was there and didn't get what i want
 
Code:
        // Related Posts Order
        if( ! empty( $query_args['order'] ) && strpos( $query_args['order'], 'related' ) !== false  ){

            $related_type = $query_args['order'];

            // Exclude the Current Post from the related posts
            $query_args['exclude_posts'] = get_the_id();

            // Unset the attrs
            unset( $query_args['id'] );
            unset( $query_args['order'] );

            // Related By Author
            if( $related_type == 'related-author' ){
                $query_args['author'] = get_the_author_meta( 'ID' );
            }

            // Related By Tags
            elseif( $related_type == 'related-tag' ){

                $post_tags = get_the_terms( get_the_id(), 'post_tag' );

                if( ! empty( $post_tags ) ){
                    foreach( $post_tags as $individual_tag ){
                        $tags_ids[] = $individual_tag->term_id;
                    }

                    $query_args['tags_ids'] = $tags_ids;
                }
            }

            // Related by Cats
            elseif( $related_type == 'related-cat' ){

                $category_ids = array();
                $categories   = get_the_category( get_the_id() );

                foreach( $categories as $individual_category ){
                    $category_ids[] = $individual_category->term_id;
                }

                $query_args['id'] = $category_ids;
            }
        }
 
anyone :)
 
up
 
Let me explain clearly if i post any normal post on my site it works great With all the theme features like the article image, author information, suggested articles, and more

But the plugin uses a custom type called glossary, i use this code

Code:
function add_glossary_post_type_support( $supported_post_types ) {

    if ( ! is_array( $supported_post_types ) ) {

        $supported_post_types = array();

    }

 

    $supported_post_types[] = 'glossary';

    return $supported_post_types;

}

add_filter( 'TieLabs/Settings/default_post_types', 'add_glossary_post_type_support' );


and this code makes my plugin get almost everything from the theme if I change the post layout It also affected the plugin post, but I only have one problem, my custom post glossary didn't get Suggested articles Feature from the theme, like what I have on the normal post
Okay I think I am beginning to understand what u mean. But I am not sure.

So please correct me if I'm wrong:
When you post a Glossary, you want the Glossary post type to also show features like: Suggested Articles and other widgets you would typically see on the theme's sidebar/other widget locations as if it were a default Post/Page post type right right?
 
Back
Top