Moodle form not showing when I make moodle header footer hidden - php

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.

Related

php how to reload "require" include file after submit in self submitting form

I am making a self processing php form. I have a header.inc and footer.inc both called with a require statement. My problem lies in my header uses a $title variable which I would like to update after post. Currently my variable is changing and I can echo it in my response (thank you) page, but the header does not reload to reflect the change.
I understand I could call the title in my main PHP file, but I was hoping to find a way to reload the require(header.inc) file and continue processing the form.
<?php
$title = "XX";
$title2 = "Form";
$thisScript = htmlspecialchars($_SERVER['PHP_SELF']);
require("htmlHead.inc");
function changeTitle()
{
$GLOBALS['title2'] = "Results";
}
changeTitle();
if (!isset($_POST['submit']))
{
echo <<<FORMDOC1
<form action="$thisScript" method="post" action=" "
onSubmit="window.location.reload()">
FORMDOC1;
else
{
//random code
}
require("htmlFoot.inc");
?>
header.inc file in question:
<?php
echo "<!doctype html> <!-- Author: $author -->\n";
echo "<html lang=\"en\"><!-- Date Written: $dateWritten -->\n";
echo "<head> <!-- Description: $description -->\n";
echo "\t<title>$title</title>\n";
echo "\t<title>$title2</title>\n";
echo "\t<meta charset=\"utf-8\" />\n";
echo "\t<link rel=\"stylesheet\" href=\"css.css\" />\n";
echo "</head>\n";
echo "<body>\n";
echo "<div id=\"wrapper\">\n";
echo "<h1>$title</h1>\n";
echo "<h2>$title2</h2>\n";
?>
You are setting the title after including the header. Therefore, the value hasn't changed yet at the time php has output the header content.
Try following this general structure
if(isset($_POST['submit')) {
//process the form
include('header.inc');
//display confirmation
}
else {
include('header.inc');
//display the form
}
include('footer.inc');

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');

PHP script tags

Why does this if statement have each of its conditionals wrapped in PHP tags?
<?php if(!is_null($sel_subject)) { //subject selected? ?>
<h2><?php echo $sel_subject["menu_name"]; ?></h2>
<?php } elseif (!is_null($sel_page)) { //page selected? ?>
<h2><?php echo $sel_page["menu_name"]; ?></h2>
<?php } else { // nothing selected ?>
<h2>Select a subject or a page to edit</h2>
<?php } ?>
Because there is html used. Jumping between PHP and HTML is called escaping.
But I recommend you not to use PHP and HTML like this. May have a look to some template-systems e.g. Smarty or Frameworks with build-in template-systems like e.g. Symfony using twig.
Sometimes its ok if you have a file with much HTML and need to pass a PHP variable.
Sample
<?php $title="sample"; ?>
<html>
<title><?php echo $title; ?></title>
<body>
</body>
</html>
This is not much html but a sample how it could look like.
That sample you provided us should more look like....
<?php
if(!is_null($sel_subject))
{ //subject selected?
$content = $sel_subject["menu_name"];
}
else if (!is_null($sel_page))
{ //page selected?
$content = $sel_page["menu_name"];
}
else
{ // nothing selected
$content = "Select a subject or a page to edit";
}
echo "<h2>{$content}</h2>";
?>
You could echo each line of course. I prefer to store this in a variable so I can easy prevent the output by editing one line in the end and not each line where I have added a echo.
According to some comments i did a approvement to the source :)
Because the <h2> tags are not PHP and will display an error if the PHP Tags are removed.
This code will display one line of text wrapped in <h2> tags.
This is called escaping.
Because you cannot just type html between your php tags.
However, I would rather use the following syntax because it is easier to read. But that depends on the programmers opinion.
<?php
if(!is_null($sel_subject))
{ //subject selected?
echo "<h2>" . $sel_subject["menu_name"] . "</h2>";
}
elseif (!is_null($sel_page))
{ //page selected?
ehco "<h2>" . $sel_page["menu_name"] . "</h2>";
}
else
{ // nothing selected
echo "<h2>Select a subject or a page to edit</h2>";
}
Because inside the if-statement there is an HTML code, which you can put it by closing PHP tags and open it again like this:
<?php if(/*condition*/){ ?> <html></html> <?php } ?>
or:
<?php if(/*condition*/){ echo '<html></html>' ; }
That is because in this snippet we see html and php code. The code <?php changes from html-mode to php-mode and the code ?> changes back to html-mode.
There are several possibilites to rewrite this code to make it more readable. I'd suggest the following:
<?php
//subject selected?
if (!is_null($sel_subject)) {
echo "<h2>" . $sel_subject["menu_name"] . "</h2>";
//page selected?
} elseif (!is_null($sel_page)) {
echo "<h2>" . $sel_page["menu_name"] . "</h2>";
// nothing selected
} else {
echo "<h2>Select a subject or a page to edit</h2>";
}
?>
using the echo-command to output html, you don't need to change from php-mode to html-mode and you can reduce the php-tag down to only one.

Allow php in wordpress page

I want to use the following code in my wordpress. How can I do that?
<?php
$theme_name = get_template();
$directory = "wp-content/themes/" . $theme_name . "/pdf/ECE340/";
$pdfs= glob($directory . "*.pdf");
foreach($pdfs as $pdf)
{
$link= substr($pdf,48,90);
?>
<?php echo $link; ?>
<?php
echo "\n\n";
echo "<br />";
}
?>
Below is the answer (taken from here):
add_filter('widget_text','execute_php',100);
function execute_php($html){
if(strpos($html,"<"."?php")!==false){
ob_start();
eval("?".">".$html);
$html=ob_get_contents();
ob_end_clean();
}
return $html;
}
Copy the above function at the end of your functions.php file.
But the best soluttion will be to create a custom template instead of using PHP in pages/widgets/posts. You will find more about wordpress custom templates here.
Login to your admin panel. Click on Appearance -> Editor from left side menu. Then find the Template where you want to add the PHP code from right side menu. Then you can add your PHP code in the editor.
NOTE: Make sure do backup before you do any editing. I'm not responsible for your mistakes.

Wordpress dynamic navigation function for highlighting single post tabs

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

Categories