John Davidson

php - how to write custom function in wordpress theme

0 comments
Message:


how to write a custom function in WordPress just like Codeigniter or laravel?
I want to access the custom function like as website.com/custom_function.


function custom_function()
{
echo "this is a custom function.";
}

please give me the way thanks.



My Answer:
To create a custom function in a WordPress theme, you can follow these steps:

1. Open your theme's functions.php file. This file is located in the wp-content/themes/your-theme-name/ directory.

2. Add the following code to create a custom function:

php
function custom_function() {
echo "This is a custom function.";
}


3. Save the functions.php file.

4. To access the custom function on your website, you can create a custom page template or use a shortcode. Here's an example of how to create a custom page template:

- Create a new file in your theme directory and name it custom-function-template.php.
- Add the following code to the custom-function-template.php file:

php
/*
Template Name: Custom Function Template
*/
get_header();

custom_function();

get_footer();
?>


- Save the custom-function-template.php file.

5. Create a new page in the WordPress admin dashboard and assign the Custom Function Template to it.

6. Visit the page on your website (e.g., website.com/custom-function) to see the output of the custom function.

That's it! You have now created a custom function in your WordPress theme and accessed it on your website.

Rate this post

4 of 5 based on 9828 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us