I work in a WP project using template parts, as they are multiple buttons instance in this project i created a button template that can take multiple arguments as such:
<?
/**
* Default button template
*
*/
$container_classes = $args["container_classes"]; //additional container classes
$button_type = $args['button_type']; // link | button | modal
$elem_classes = $args['elem_classes']; //additional classes for the button
$url = $args['url'] ?? null; //provides the url target
$text = $args['text'] ?? null; // text to display inside the button
$text_color = $args['text_color'] ?? "white"; // text color
$bg_color = $args['bg_color'] ?? 'cta';
$target = $args['target'] ?? "_self"; //target attribute
$icon = $args['icon'] ?? null; //If there is an icon, link to the icon
$modal_slug = $args['modal_slug'] ?? 'null'
?>
<div class="button-<?= $button_type ?> flex <?= $container_classes ?>">
{... All the button specifications and other logical checks ... }
</div>
I would now like to use this template in multiple sections. But with all those $args i would like to have suggestions when typing in the arguments in the get_template_part() function.
Is there a way of defining the possible $args in the template-part file as i've seen it being used in functions for instance ?
Exemple of predefined params for a function:
function get_template_part( $slug, $name = null, $args = array() ) {
/**
* Fires before the specified template part file is loaded.
*
* The dynamic portion of the hook name, `$slug`, refers to the slug name
* for the generic template part.
*
* @since 3.0.0
* @since 5.5.0 The `$args` parameter was added.
*
* @param string $slug The slug name for the generic template.
* @param string|null $name The name of the specialized template.
* @param array $args Additional arguments passed to the template.
*/
where the @param notation defines expected parameters.
I'm trying to have suggestion proposed by VsCode when using a template part and trying to figure out a way for the $args to reproduce the params behavior in the case of a function.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.