My code crashes Elementor for me easily (WP + PHP ) - php

I wrote a code for a loop but it disrupts everything for me, I have a file
file.php that is inside my template (let's say it's called test)
Now I call the file from the function (function file by include_once ('file.php'); )
Now it works, but my elementor display is broken and I can't update anything, and it also ruins my main pages, but the post pages look great
<?php
function video() {
echo '<link rel="stylesheet" href="/style.css">';
$x = 1;
$number = get_field_object('number');
echo '<div class="col-lg-2-1">
<div class="carousel">
<div class="video"><iframe src="" allowfullscreen frameborder="0" name="slider1"></iframe></div>
<span class="kfir">';
while($x <= $number['value']) {
$prak = 'video '.$x;
$num = 'chapter_'.$x;
$chapter1 = get_field_object($num);
echo '<div class="text">'.$prak.'</div>';
$x++;
}
echo '
</span>
</div>
</div>';
}
add_shortcode('video_live', 'video');
?>
Does anyone know where my mistake is?
And can someone guide me how to make it work only within the posts pages?

Related

jQuery load() can't load php page with text file?

jQuery code:
function sorting(){
$(".search_result_employee").load("include/searchedResult.php", {sort: "SORT_DESC"});
}
php code:
$array = file("data/employees.txt");
$row = intdiv(count($array), 4);
if (count($array)%4 != 0){
$row++;
}
//$row = round(count($array)/4);
$x = $z = 0;
for ($x; $x < $row; $x++){
echo '<div class="row my-4">';
for ($y=0; $y <= 3; $y++){
if (isset($array[$z])) {
$name = explode("| ", $array[$z]);
$eName = $name[0];
$eDepartment = $name[7];
$eProfile = end($name);
echo '
<div class="col-md">
<a href="displayInfo.php?name='."$eName".'" class="grey_bg p-2 m-1 employeeCard">
<img src="employeesProfile/'."$eProfile".'" alt="profile" class="profile"/>
'."$eName".'<br/>
<span>'."$eDepartment".'</span>
</a>
</div>
';
}
$z++;
}
echo '</div>';
}
First go in the page using
<?php include("include/searchedResult.php"); ?>
The page can found the file. Howver, after clicked the button, the jQuery load() the include/searchResu;t.php page. Then, the file doesn't exist. First time is working properly thensecond time can't. Anyone know how to solve this?
this is the error:
Warning: file(data/employees.txt): failed to open stream: No such file or directory in C:\xampp\htdocs\COS30020\include\searchedResult.php on line 14
When including files in PHP, all relative paths will be relative from the first entry PHP-file.
If index.php includes searchResult.php, then file('data/employees.txt') will be relative from the index.php-file, not the searchResult.php-file.
If you call the searchResult.php-file directly, then it will be relative from searchResult.php (which then is the entry file).
Solve this by using the magic constant __DIR__, which contains the absolute path to the file it's written in.
So it should be:
file(__DIR__ . '/relative/path/from/this/file/data/employees.txt')

How correctly refresh included PHP file?

I'm just a persistent beginner and I've met another obstacle in my way, so I hope that you'll help me one more time... :) I've got that HTML:
<div class='hold'><?php include 'image.php'; ?></div>
<div class='refresh'>Get new image</div>
And that PHP code:
<?php
$dir = 'images/';
$img = array();
if(is_dir($dir)) {
if($od = opendir($dir)) {
while(($file = readdir($od)) !== false) {
if(strtolower(strstr($file, '.'))==='.jpg') {
array_push($img, $file);
}
}
closedir($od);
}
}
$id = uniqid();
$smth = array_rand($img, 1);
echo '<img src=' . $dir.$img[$smth] . '?' . $id . ' width="200px" height="50px" />';
echo '<input type="hidden" value=' . $id . ' />';
?>
So now when I'm looking at my page I see in the <div class='hold'></div> my img from the folder images, and it's allright. BUT when I click on the <div class='refresh'></div> I obviously want to get another img from my folder, but I dunno how to accomplish that trick correctly.
I know that first answer will be USE AJAX, and I know that I can do something like
function loadGraphic() {
$('.hold').load('image.php');
};
loadGraphic();
and then $('.refresh').click(loadGraphic); but when I'm trying to do that in response from server I get TWO THINGS: image.php and, of course, something like car.jpg?573c4e010c7f6... But I very-very wanna get just ONE looking like
car.jpg?573c4e010c7f6
or
image.php?573c4e010c7f6 - I don't care...
So... I hope you've got my concept... maybe I'm asking for miracle - I dunno! Any, absolutely any help will be appreciated :)
Try it the following way:
JS function:
function loadGraphic() {
$.get("image.php", function(data) {
$(".hold").html(data);
});
};
Html:
<div class='refresh' onclick='loadGraphic()'>Get new image</div>

Wordpress: How Can You Get The URL Of The Next Child Page In A Sequence Of Child Pages

I'm finishing up my new portfolio and am trying to figure out a way to get a 'next' and 'prev' URLs for the child pages of the parent, which is called 'Work'.
The reason I want URLs is because the actual clickable link is an SVG at larger viewport sizes and I want to maintain all of my css access to the SVGs.
I'm including an image of how the navigation looks (looks like an aside, but it's actually a outside of the ).
I know that pages in worpress 'aren't meant to be paginated' but this is the same thing as creating a new post type and using the pagination there, except im doing some other things where I want access to templates. I tried using this plugin:
http://binarym.com/2009/next-page-not-next-post/
Which works, but I can't get my SVGs in there as opposed to text. If anyone has a way to replace the text in those strings with my SVG paths, that's an acceptable fix too. Here's a code snippet of what that looks like using that plugin:
<nav role="navigation" class="project-pagination">
<a href="/work">
<?php include (TEMPLATEPATH . '/images/_svgs/nav_gallery.svg'); ?>
</a>
<?php
$nextPage = next_page_not_post('Next Page', 'true', 'sort_column=post_date&sort_order=desc');
$prevPage = previous_page_not_post('Previous Page', 'true', 'sort_column=post_date&sort_order=desc');
if (!empty($nextPage) || !empty($prevPage)) {
if (!empty($nextPage)) echo $nextPage;
if (!empty($prevPage)) echo $prevPage;
}
?>
</nav>
Thanks all!
I found the answer here: Magic Town
<nav role="navigation" class="project-pagination">
<a href="/work" aria-label="View All Projects" alt="View All Projects">
<?php include (TEMPLATEPATH . '/images/_svgs/nav_gallery.svg'); ?><span>View All Projects</span>
</a>
<?php
$pagelist = get_pages("child_of=".$post->post_parent."&parent=".$post->post_parent."&sort_column=menu_order&sort_order=asc");
$pages = array();
foreach ($pagelist as $page) {
$pages[] += $page->ID;
}
$current = array_search($post->ID, $pages);
$prevID = $pages[$current-1];
$nextID = $pages[$current+1];
?>
<?php if (!empty($nextID)) { ?>
<?php include (TEMPLATEPATH . '/images/_svgs/nav_next.svg'); ?><span>Next Project</span>
<?php }
if (!empty($prevID)) { ?>
<?php include (TEMPLATEPATH . '/images/_svgs/nav_prev.svg'); ?><span>Previous Project</span>
<?php } ?>

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.

PHP function return. Cheats allowed

OK.
I have a very long and pretty complicated function.
It looks almost like this one:
<?php
function hello() {
echo 'My Function!' ?>
<ul>
<li> Blablabla </li>
<ul>
(...)
<?php } ?>
The HUGE problem here is that I'm UNABLE to echo anything.
My function HAVE to return it's contents instead of echoing or direct outputting (it has to be that way, it's a Wordpress shortcode and when I echo - the contents are getting displayed at the top of the page - ALWAYS, not in the place where I want them):
<?php
function hello() {
$output .= 'My Function!';
$output .= '<ul>';
$output .='<li> Blablabla </li>';
$output .='<ul>';
(...)
return $output;
} ?>
I hope it's easy till now.
Now, the real problems are:
I have tons of direct input code like:
?>
<div>
<span>
<p>Smth</p>
<a>smth</a>
</span>
</div>
<?php
Adding $output everywhere kills the nice paragraphs/whitespace and code is getting VERY HARD to read and understand (and all HTML elements are parts of variable now, so even my php editor is not treating them well and coloring them as PHP elements).
And another thing, I have tons of lines like this one:
<a href="<?php bloginfo('template_directory'); ?>/includes/php/timthumb.php?src=<?php echo $url; ?>&h=<?php if($items=="one") echo 320; elseif($items=="two") echo 230; elseif($items=="three") echo 180; elseif($items=="four") echo 130; ?>&w=<?php if($items=="one") echo 600; elseif($items=="two") echo 420; elseif($items=="three") echo 277; elseif($items=="four") echo 203; ?>" title="<?php the_title(); ?>" class="link">
(yes, this is a single line)
And I have absolutely no idea how to add such lines to $output.
$output .= '<a href="<?php bloginfo('template_directory'); ?>/includes/php/timthumb.php?src=<?php echo $url; ?>&h=<?php if($items=="one") echo 320; elseif($items=="two") echo 230; elseif($items=="three") echo 180; elseif($items=="four") echo 130; ?>&w=<?php if($items=="one") echo 600; elseif($items=="two") echo 420; elseif($items=="three") echo 277; elseif($items=="four") echo 203; ?>" title="<?php the_title(); ?>" class="link"> ';
Doesn't work of course (even with \'s before ' and ").
I believe there MUST be an easier way to attach all the code to return, but how?
I've tried with ob_start(); before code and return ob_get_clean(); after, but it outputs shortcode name instead of contents.
Its very hard to imagine what the problem you are trying to solve here is - although I'm not familiar with wordpress. Can't you just call the function where the output is supposed to go?
You could use output buffering - use echo/print as usual but...
ob_start();
hello();
$output=ob_get_contents();
ob_end_clean();
But that doesn't solve the problem that you still need to send to the browser at the right place in the page - and if you can do:
print $output;
in the right place, then you can surely do:
hello();
in the same place.
I agree with symcbean, but this might be a more practical approach at integrating with Wordpress: if you now have a single function called hello( ) which displays HTML, you might want to consider renaming that function to hello_content( ) (or something similar) and replace the hello( ) function with the suggestion symcbean gave you:
function hello_content( ) {
echo "foo";
}
function hello( ) {
ob_start( );
hello_content( );
return ob_get_clean( );
}
That should fix your immediate issue.
PHP Heredoc syntax will keep things looking neat and tidy and you can return the output to a variable. As long as you don't require any constants it will work fine.
You use it in this fashion:
function bar() {
$var = <<<EOV
anything here
anything there
EOV;
return $var;
}

Categories