I am using a licenced version of the WordPress Grooni Crane theme
https://crane.grooni.com/promo/
https://grooni.com/docs/crane/search/crane_before_primary_menu_area
I want to add content and images to a section ABOVE the Groovy Menu Plugin (comes with the Grooni theme).
So usually I would create te HTML of the images and content and forcefully add it into the header.php file before the menu gets shown, but upon doing this I saw that the PHP now allows for a content section to be added / displayed before the menu will be displayed using crane_before_primary_menu_area .
My question is: How / where in the Grooni Crane theme do I create / edit this section, so that I dont have to force the HTML into the header file, but so that it rather get added dynamicly using the proper functions.
Code in the header.php file looks like this:
do_action( 'crane_before_primary_menu_area' );
do_action( 'crane_primary_menu_area' );
do_action( 'crane_after_primary_menu_area' );
So it shows 'crane_before_primary_menu_area' first, then it shows the menu bar, and then it should show 'crane_after_primary_menu_area'.
My Question is where / how (on the wordpress CMS) do I create / edit a content section for 'crane_before_primary_menu_area' & 'crane_after_primary_menu_area'
This must be done through the child theme.
Together with the Crane theme It was bundled.
Activate it or create your own new one.
https://developer.wordpress.org/themes/advanced-topics/child-themes/
Insert the hook into the functions.php file that will be called in the action 'crane_before_primary_menu_area'
Sample contents of functions.php for child theme
<?php
if ( ! function_exists('custom_func__crane_before_primary_menu_area')) {
add_action( 'crane_before_primary_menu_area', 'custom_func__crane_before_primary_menu_area' );
function custom_func__crane_before_primary_menu_area() {
$html_content = '<div>Content before menu area</div>';
echo $html_content;
}
}
In the above example, we assigned HTML text to the variable
$html_content = '<div> Content before menu area </div>';
And immediately it was shown to the front
echo $html_content;
However inside the function
function custom_func__crane_before_primary_menu_area () {.......}
you can call any other available functions, and display any content you wish.
Related
I've recently created a child theme for OceanWP using elementor page builder.
I have uploaded the folder through Filezilla FTP put in the theme's folder next to the parent. I created style.css and functions.php and added the appropriate code into each and all pages of the site load fine after clearing the cache on WP Rocket and Cloudflare CDN.
I then went to add a footer.php file (because I need to add some additional code to the file just before the </head> tag for an affiliate link) and this is where I got confused.
Should I copy the code from the parent footer.php and insert it into the child? (I did this and my margins got all messed up and css styling went beserk)
Should it be left blank until I add code that I need? (I did this and my footer disappeared and menus wouldn't work properly?!)
Should it be neither and do I need to add some sort on action or enqueue script in the functions.php of the child theme to allow footer to appear properly?
I'm doing this all so when and if I update the theme, these snippets of code aren't deleted from my server and site entirely.
Do I even need to do this and should I just add it to the parent footer.php file?
I'm very new to coding and know only small amounts of HTML,CSS and just starting to learn PHP.
Here's the code I have in my child theme function.php file
/**
* Child theme functions
*
* When using a child theme (see http://codex.wordpress.org/Theme_Development
* and http://codex.wordpress.org/Child_Themes), you can override certain
* functions (those wrapped in a function_exists() call) by defining them first
* in your child theme's functions.php file. The child theme's functions.php
* file is included before the parent theme's file, so the child theme
* functions would be used.
*
* Text Domain: oceanwp
* #link http://codex.wordpress.org/Plugin_API
*
*/
/**
* Load the parent style.css file
*
* #link http://codex.wordpress.org/Child_Themes
*/
function oceanwp_child_enqueue_parent_style() {
// Dynamically get version number of the parent stylesheet (lets browsers re-cache your stylesheet when you update your theme)
$theme = wp_get_theme( 'OceanWP' );
$version = $theme->get( 'Version' );
// Load the stylesheet
wp_enqueue_style( 'child-style', get_stylesheet_directory_uri() . '/style.css', array( 'oceanwp-style' ), $version );
}
add_action( 'wp_enqueue_scripts', 'oceanwp_child_enqueue_parent_style' );
You wrote: "I then went to add a footer.php file (because I need to add some additional code to the file just before the head tag for an affiliate link) and this is where I got confused."
"just before the tag head" has nothing to do with footer.php
Usually,
the file footer.php has something to do with "just before the tag body"
the file header.php has something to do with "just before the tag head"
I say usually, because in some themes header.php and footer.php call other files or functions.
So, first of all, be sure you are not confusing header.php with footer.php
If your goal is writing additional code before the tag head, copy header.php and put it in your child theme. Then modify that file.
I'm trying to create a custom template for a small wordpress site, the template should display some data from the database.
But I can't get PHP to output anything on the pages, I've written a few echo's just to get something but can't see anything.
I can, however, add the page through "Pages" in WordPress, that works just fine and displays an empty page (or with text if I write something in the text box)
To set up the template I created a child template of my original template, the CSS file in the child template works just fine.
I added the custom page called "petitionlist.php" in the root of my child-theme, the code I added for testing is as following
<?php
/*
Template Name: petitionlist
*/
get_header();
echo "test test test";
$test = "testtetsttest";
echo "<h1> this is a test</h1>".$test; ?>
<h1>Hello wordl!</h1>
<?php get_footer(); ?>
Attached is a screenshot of my folder-structure, the petitionlist.php file in wechange-child is the file I'm working on.
I have made a simple shortcode for my wordpress theme. I'm trying to align it on right-side above the corner of my page. Is it possible to insert shortcode function.php?
I want to place my Short-code in the top-bar (right side above the corner) so that it will work for each pages. Means if i click on any menu then the shortcode should have to work on that menu also.
Actually i'm new in web development. Please suggest me what shall i have to do so it will align where i want and should have work for each module.
You can use wordpress function for that. Please open the file on which you have to place shortcode. Like if you want to place it on header open header.php and add
<?php echo do_shortcode('[your_shortcode]'); ?>
where you want your shortcode content to appear
The best way is to find the top bar menu hook of your theme and then add this code (adapted for your needs) in your function.php child theme :
function my_shotcode_inside_top_bar ( $items, $args ) {
if (is_page() && $args->theme_location == 'secondary') {
$items .= '<li class="my-class">' . do_shortcode( "[my_shortcode]" ) . '</li>';
}
return $items;
}
add_filter( 'wp_nav_menu_items', 'my_shotcode_inside_top_bar', 10, 2 );
is_page displays the shortcode in pages only but you can choose another condition or no condition at all.
secondary is the hook and it depends on your theme. You can normally find it in your theme header.php file.
A menu location selector should be added - that way some themes support multiple menus, and some are called "top" or custom added.
The shortcode could then contain a call to whatever a menu position is:
EG: [myshortcode menu="secondary"]
Didn't have enough points to add this as a comment.
I have a .php file which uses some WP functions e.g. get_stylesheet_directory_uri(), query_posts, etc... and $wpdb of course.
File it self return JSON data so it is not intended for viewing on its own.
I'm not looking for template, or clicking around in WP dashboard, I just want to know where is the good place in WP file structure to put the .php file and expect WP functions and object to be available.
And also what do I need to include at the top?
You can create custom template in theme root directory or in child theme (child theme is important when you are creating custom template).
template should contain template name, header and footer function.
eg. custom-tpl.php
<?php
Template Name: My Custom Temlate
get_header();
//your other functions and content goes here
get_footer();
after creating template it will appear in your edit page template section under drop down. select custom template for your page and view page you will get your output
You can create a custom page template.
Inside the page template:
<?php
global $wpdb;
[here you will write your code]
?>
On the front pages where you want the json output, use this url
http://domainname.com/wp-content/themes/themename/custompagetemplatename.php
Use your domain name (domainname), theme name (themename) and theme template file name (custompagetemplatename) in place of example names in the url. This url will return you the output for your code.
About this comment in your template:
<?php
/* Template Name: Full Width Page */
?>
You do not need a template name in the header of your file unless you want to use this as a template for pages in wordpress.
I need to include a custom PHP page in Wordpress.
So what I need to do is just to show this custom php page using the Wordpress theme installed on that Wordpress.
Does not mind which theme is up, the custom php page will have to be shown under any theme is installed in that moment.
How do I do it in Wordpress?
I am new to Wordpress development.
Thanks
Creating a custom php page that will be able to be viewed in any theme (and have the theme applied) would be considerably difficult.
Each wordpress page calls specific theme functions of that particular theme, as well as referencing files of that theme to generate header, footer, css files, javascript files, etc.. Your custom page would need to plan for all of these contingencies, for each possible theme used.
Here's a alternative solution: inject PHP code directly into a standard wordpress page via this plugin http://wordpress.org/extend/plugins/allow-php-in-posts-and-pages/
Meaning: you make a normal wordpress page, but are able to add php to it. When this page is rendered, the proper page template is used, and all the theme references are taken care of for you.
You could do this easily with a page template. WordPress allows you to create page templates which can be assigned to a page via the 'Page Attributes' panel within the page editor. These templates are php files inside your theme directory which begin with some code like (see this page in The Codex for more info):
<?php
/*
Template name: Custom PHP Page
*/
?>
<?php // begin custom PHP page ?>
Typically a template is a variation on the regular theme files (such as page.php) and would call the get_header() and get_footer() functions and have an instance of the loop. However if you simply want to use a custom PHP page, then all you need to do is create the file you want inside the current theme directory and add the above code at the very top of the file.
To output the custom PHP page on your site, you would need to add a new page via the admin area and then assign your new page template to this page.
Alternatively, if you want to include a custom PHP page inside an existing theme file, you use the code:
<?php include(TEMPLATEPATH . '/includes/file.php'); ?>
in this case your custom PHP file would be located inside a directory called 'includes' within your current theme directory.
Tim.
It's not that difficult. Here's what you need:
Once you include the main wordpress blog header, the entire armamentarium of wordpress functions is available to you, which allows you to get the active theme's directory. Once you get that, just include the header and the footer of the theme.
// If title is not displayed before loading the header, Wordpress displays "Page not found" as the title
echo "<head>
<title>Your page title</title>
</head>";
// Include the Main Wordpress blog header
include $_SERVER['DOCUMENT_ROOT']."/wp-blog-header.php";
//Now, you need to get the active theme's folder, and get a relative path to that folder
$homeurl=home_url();
$ddir= get_bloginfo( 'template_directory');
$current_theme_relative_path=substr_replace($ddir, "", 0, strlen($homeurl));
//echo "<br/>The relative path to the currently active theme is ".$current_theme_relative_path;
//Once you have the path, include the header and footer, adding your custom php code in between.
// Include the specific theme header you need
include $_SERVER['DOCUMENT_ROOT'].$current_theme_relative_path."/header.php";
// Your custom PHP code STARTS here
// Add anything you want to display to the user
echo "
<h2>
Your form has been submitted
</h2>";
// END of custom code
?>
<?php
}
// Now end with the theme's footer
include $_SERVER['DOCUMENT_ROOT'].$current_theme_relative_path."/footer.php";
?>
Was very helpfull (even if dated of 2011-13)
Also, as a thank you, i'm sharing the version i made
it's usefull if your wordpress folder is not located at ROOT
PasBin Link - wordpress custom php page
just change the value of $wplocalpath in :
// Wordpress path (if wordpress is not located at ROOT
// $wplocalpath="/Wordpress1";