Wordpress dynamic navigation function for highlighting single post tabs - php

I am trying to write a function which I can re-use in my WordPress themes that will allow me to build robust dynamic navigation menus. Here is what I have so far:
function tab_maker($page_name, $href, $tabname) {
//opens <li> tag to allow active class to be inserted if tab is on proper page
echo "<li";
//checks that we are on current page and highlights tab as active if so
if(is_page($page_name)){
echo " class='current_page_item'>";
}
//closes <li> tab if not active
else {
echo ">";
}
//inserts the link as $href and the name of the tab to appear as $tabname then closes <li>
echo "<a href=$href>$tabname</a>";
echo "</li>";
}
This code works as expected except I cant enable it to highlight for a single blog post as the page names are dynamic.
I know about the WordPress function is_single() which I've used to implement this feature in previous nav menus but I can't find a way to integrate it into this function.

I can see were your going with this,
inside your if statement for the is_page
can you use,
function tab_maker($name, $href, $tabname) {
if(is_page($name)){
echo " class='current_page_item'>";
}else **if(is_single($name)){
echo " class='current_page_item'>";**
}else{
echo ">";
}
echo "<a href=$href>$tabname</a>";
echo "</li>";
}
haven't tried this myself

Related

php - How to use jquery correctly in .php files? WP-Admin only showing html

I have a wordpress installation where I am currently trying to create a formular where after sending the formular I process the data and save it into a mysql DB.
To achieve this I have created a new .php file which I included in the "function.php" of my theme because i want to add the whole formular/process with a shortcode.
The .php file currently contains this (Simplified):
function FNC_Add_New_Record ()
{
$options = "<option value='unselected'>- Select one -</option>"
. "<option value='option1'>Option 1</option>"
. "<option value='option2'>Option 2</option>";
echo "<br>";
echo "<form id='form1' method='post'>";
echo "<div style='text-align: center;'>";
echo "<font size=5>Selection 1</font><br>";
echo "<input type='text' name='textfield1' placeholder='Insert caption here'><br>";
echo "<select id='selection1'>" . $options . "</select>";
echo "<select id='selection2'><option value='unselected'>- No option1 selected -</option></select>";
echo "<br><br>";
echo "</div>";
echo "</form>";
echo "<br>";
}
add_shortcode('FNC_Add_New_Record','FNC_Add_New_Record');
Because the selection 2 depends on what is selected with the selection 1 I added jquery to the end of the .php file:
?><!DOCTYPE html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script type = "text/javascript" language = "javascript">
$(document).ready(function () {
$("#selection1").change(function () {
var val = $(this).val();
if (val == "option1") {
$("#selection2").html("<option value='none'>None</option><option value='option2'>Option 2</option>");
} else if (val == "option2") {
$("#selection2").html("<option value='none'>None</option><option value='option2'>Option 2</option>");
} else if (val == "unselected") {
$("#selection2").html("<option value='unselected'>- No option1 selected -</option>");
}
});
});
</script>
So, there is probably a lot I have done wrong but this was the only way I was able to get it working. Yes I have searched here and on a lot other sides for proper solutions which tell me to add it to the function.php of the theme and do it with "enqueue". I have tried all of the possible suggest functions and code snippets but nothing worked.
The issue what I have with this solution is that it renders the wp-admin of my wordpress installation useless because it just shows the code which I have added to the .php file as html.
what I get on my site accessing wp-admin:
Also i'd like to note that using the tag "<!DOCTYPE html>" is a solution to another problem I have when adding jquery but i have no idea if it's the correct solution. If I remove this tag the main navigation of my website, instead of being like 15px in height suddenly has a height of almost a whole full HD screen.
Wordpress function.php also includes all JS files, make use of wp_enqueue_scripts to queue up your scripts.
function my_scripts_loader() {
wp_enqueue_script('myjquerylib-js', get_template_directory_uri() . '/assets/js/myjquerylib.js');
}
add_action('wp_enqueue_scripts', 'my_scripts_loader');

Moodle form not showing when I make moodle header footer hidden

I have moodle page like the following with a form inside it:
<?php
require_once('../../config.php');
global $DB;
global $COURSE;
$courseid = $COURSE->id;
if (!$course = $DB->get_record('course', array('id' => $courseid))) {
print_error('invalidcourse', 'block_eparticipation', $courseid);
}
require_login($course);
$PAGE->set_url('/blocks/eparticipation/view.php', array('id' => $courseid));
$PAGE->set_pagelayout('standard');
echo $OUTPUT->header();
require_once(dirname(__FILE__).'/epoll_form.php');
$pollform = new mform("poll_action.php?val={$valid}");
$pollform->display();
echo $OUTPUT->footer();
?>
I need to hide the header and footer so I did as follows:
echo "<div style='display:none;'>";
echo $OUTPUT->header();
echo "</div>";
.............................
echo "<div style='display:none;'>";
echo $OUTPUT->footer();
echo "</div>";
Or we can use : hide_header();
But When I did this my form also get hidden. I am getting a blank page.
I want to hide the header and footer only with my form not hidden.
Is there any alternatives to hide moodle header and footer?Please guide me.
Moodle Version : 2.9
Call:
$PAGE->set_pagelayout('popup');
Before you call echo $OUTPUT->header(). This will hide the header and the page blocks.
You must call echo $OUTPUT->header() on every Moodle page, as that includes the HTML head tag, with all the required CSS, javascript, etc needed by Moodle (as well as the visible 'header' elements).
See: https://docs.moodle.org/dev/Page_API for full details of the $PAGE variable.

Best way to have a variable template in php

I'm coding a menu bar for a website in php. Because I don't want to have to edit it multiple times on the half a dozen or so pages I'll have I've decided to put it in it's own separate header.php file and just include_once(header.php) in the various pages.
My problem is that the menu is going to be slightly different depending on which page it's included in. Right now I'm dealing with it by having the following in my header.php file with $PageTitle being defined in the individual pages:
if ($PageTitle == "Home"){
echo '<li class="active">Home</li>';
}
else{
echo '<li>Home</li>';
}
if ($PageTitle == "About"){
echo '<li class="active">About</li>';
}
else{
echo '<li>About</li>';
}
...
The active class simply highlights the menu of the current page (Like the menu bar on the top of StackOverflow). It works fine but I'm curious if there is a better perhaps more efficient way to doing this. Thanks guys.
Try this:
//list of menu headers
$headers = new array();
//populate the array with your headers here ...
foreach($headers as $val)
{
if( $PageTitle == $val )
echo '<li class="active">'.$val.'</li>';
else
echo '<li>'.$val.'</li>';
}
for current class you can also use jquery if you want to:
$(function(){
var path = location.href;
if ( path )
$('.side_menu a[href="' + path + '"]').attr('class', 'current');
});

Plugin content always on top of post WordPress

I'm trying to make a WordPress plugin. Well, actually it's done and fully working, except one thing.
I have added a shortcode for the plugin. But no matter where in the content I call this shortcode, the contents it gets are always on top of the post, instead of where I placed the tag.
The code that outputs something:
public static function showIncomingSearches(){
global $id;
$arSearches = self::getArObj(array('wp_post_id' => $id));
ob_start();
if(!empty($arSearches)){
$str = '<ul>' . PHP_EOL;
foreach($arSearches as $oSearch){
$str .= '<li>'.htmlspecialchars($oSearch->searchterm).'</li>' . PHP_EOL;
}
$str .= '</ul>';
if(!empty($arSearches))
echo $str;
} else {
echo ' ';
}
return ob_get_clean();
}
And the shortcode functionality:
add_shortcode('show_incoming_searches', 'checkReferrer');
function checkReferrer(){
incomingSearches::checkReferrer();
echo incomingSearches::showIncomingSearches();
}
What I want to know though, is why it is always on top of the content?
Your shortcode code needs to return the content, not echo it.
function checkReferrer(){
incomingSearches::checkReferrer();
return incomingSearches::showIncomingSearches();
}

Fail to using if/else to hover image by dynamic url

I'm using the basic way to doing the hover image as the CSS method doesn't work for me. Current I'm using the if/else statement to do so. If the contain the URL like abc.com it will hover the image.
But now I only can hover the group url but if there is sub categories in groups I won't able to hover, how can I do it all the activity inside the group, the image will hover?
How to doing if the URL contain the words or path. For example abc.com/groups/* it will hover the groups. Similar like we doing searching in MySQL the words/variable as using "%".
<?php
$request_url = apache_getenv("HTTP_HOST") . apache_getenv("REQUEST_URI");
$e = 'abc.com/dev/';
$f = 'abc.com/dev/groups/';
$g = 'abc.com/dev/user/';
?>
<div class="submenu">
<?php
if ($request_url == $e) {
echo '<div class="icon-home active"></div>';
} else {
echo '<div class = "icon-home"></div>';
}
?>
<?php
if ($request_url == $f) {
echo '<div class="icon-groups active"></div>';
} else {
echo '<div class = "icon-groups"></div>';
}
?>
</div>
I propose a javascript way to do so, with jQuery
$("a[href*='THE_URL_PATTERN_YOU_WANT_TO_MATCH']").children(".icon-home").addClass("active");
BTW, it is NOT a good idea to wrap a div into a a tag.

Categories