wordpress custom html widget with PHP - javascript

I'm trying to add a php code inside a custom HTML widget but the site in showing this like plain text.
Any idea why is this happening?
PHP CODE:
<?php
$param = uniqid();
echo "<script language='javascript'>";
echo "alert('message successfully sent ' . $param)";
echo "</script>";
?>
HTML:
HOW I SEE THE PAGE:

Same problem happenned to me today!!, resolved it by adding the following code to the Wordpress theme function.php file.
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;
}
To enable shortcodes in the Text and Custom HTML widget just add this code to your theme functions.php :
add_filter('widget_text','do_shortcode',10);

Related

Adding widget to WordPress frontpage

I created a website in WordPress (Website: Link) using this theme.
I would like to know, how can I add a [timeline-express] widget after the image slider?
(I tried to modify idyllic-functions.php, using:
...
</div> <!-- end .main-slider -->';
echo $idyllic_category_sliders_display;
echo add_filter( 'widget_text', 'timeline-express');
...
,but the site crashes.)
Open this file: idyllic/inc/front-page/front-page-features.php and add this following line on line 34:
<?php echo do_shortcode('[timeline-express]'); ?>
Just above this line:
<div class="our-feature-box <?php echo esc_attr($feature_box_class); ?>">
So, your updated code will be also follows:
<?php echo do_shortcode('[timeline-express]'); ?>
<div class="our-feature-box <?php echo esc_attr($feature_box_class); ?>">
Note that, it's always a good practice to create a child theme, copy file/template to the child theme directory, and then modify your codes. Otherwise, you will lose your modified codes once the theme is updated.
PS: please try to follow what #tacoshy mentioned in his comment while asking questions in stackoverflow.

How to call a custom JavaScript alert box from php

I have the following code.
echo '<script language="javascript">';
echo 'alert("This is a popup box")';
echo '</script>';
This works fine as intended. However, I would like to use a custom and aesthetically pleasing pop-up box. So I went online and saw some nice JS libraries such as Alertify, BootBox and a few others. I included the JS scripts (JQuery,Boostrap, library scripts) and their CSS files on the top of the PHP file. Now I use the same method as previously.
echo '<script language="javascript">';
echo ' alertify.alert("This is the default alert!")';
echo '</script>';
For some reason this never works, I get the following error. Can anyone tell what I may be doing wrong, or what is the correct way to achieve this?
You need to wait for the DOM to load all the files (in this case your external script) - try this:
echo '<script language="javascript">';
echo 'document.addEventListener("DOMContentLoaded", function() {';
echo ' alertify.alert("This is the default alert!")';
echo '});';
echo '</script>';
You can as well ensure you are loading the CSS and JavaScript files in the following Order. I never have issue following this order.
I.) CSS First
BootStrap CSS
Other Vendor CSS
Your custom CSS
II.) JavaScript second
jQuery Js
BootStrap Js
Other Vendor Js
Your Custom Js

Get source code of element in PHP

I have a container which has dynamic content in it. The content can be edited by users with an inline editor called Froala Editor.
<div id="froala-editor">
DYNAMIC HTML CONTENT
</div>
I want to output the source code generated by the froala editor into a php variable.
I have tried using froala editor's function for it, but it does not get any result.
$('div#froala-editor').froalaEditor('html.get');
I also tried to write my own function:
<?php
ob_start();
?>
<script>
document.write($('div#froala-editord').html());
</script>
<?php
$output = ob_get_contents();
ob_end_clean();
$html_output = htmlentities($output);
echo "<pre>";
echo $html_output;
echo "</pre>";
?>
But this snippet gives the result:
<script>
document.write($('div#froala-editor').html());
</script>
What I want is to get the source code of the #froala-editor div and store it in a php variable.
I also tried to use file_get_contents() but the file that has this div also has dynamic content in it.
Also tried the Simple HTML DOM Parser but it gives back an error using file_get_contents()
When the user finished editing the content with the editor, I want to submit a form with a hidden element that has the value of the edited content's source code.

cakePHP scriptStart() and scriptEnd(), how to use?

why this code is not working?
Seemsm like i don't understand the usage of scriptstart() and scriptEnd().
// view
<?php
$this->Html->scriptStart(array("block"=>true,"inline"=>FALSE));
?>
$().ready(function(){
alert("dd");
});
<?php
$this->Html->scriptEnd();
?>
// layout
echo $this->fetch('script');
edit
Some more info:
I expect it to popup the alert..
In the example nothing happens. seems like the javascript is not being added to the page. (i checked the source)
Try removing the "block"=>true option, or setting it to 'script' instead:
// view
<?php
$this->Html->scriptStart(array("block"=>'script',"inline"=>FALSE));
?>
$().ready(function(){
alert("dd");
});
<?php
$this->Html->scriptEnd();
?>
Make sure you include the php tags in the layout:
// layout
<?php echo $this->fetch('script');?>

How to use php class using jquery call?

i have a problem with calling php data. Its shows me 'its works' and 'blalalala' only, without class function data. how to use class echo's calling from .load or some other way?
php file code:
echo 'its works';
echo '<br>';
echo 'blalalala';
Some_Data::getData();
script inside the html:
<script>
$(document).ready(function(){
function myShow(){$("#dd").load("/get_some_data.php")}
setInterval(function(){myShow()},2000)});
</script>
<div id="dd"></div>
my program shows me this into the browser:
its works
blalalala
So we can see that my class did not started.
And if i use my class without jquery its works perfect so problem not in my class. Its dont load data from my class. And even if i'm testing this case by using echo time(); as result i will see different time every 2 seconds but class do not starting.
php file:
class Some_Data
{
public static function getSomeData()
{
if(){ echo '<div class="my_messages">';
echo $row1['text'];
echo '</div>';
}else{
echo '<div class="their_messages">';
echo $row1['text'];
echo '</div>';
}
}}
Some_Data::getSomeData();
my class and my class calling located in the same php file so with require_once html shows me all what i need... there if no problems in class file... maby its some protection moments and i need to use .get and convert all variables into array to get them with json.parse
Try echoing the last line as you do with the previous three. If you don't echo (print_r, var_dump... etc) it won't send the information to the client.
echo 'its works';
echo '<br>';
echo 'blalalala';
echo Some_Data::getData();
If you are already doing an echo in your getData(); post your Some_Data class.

Categories

Resources