简体   繁体   English

500个内部服务器错误jQuery和WordPress

[英]500 internal server error jQuery and WordPress

I am attempting to use ajax in my WordPress project to dynamically load videos depending on their post ID. 我正在尝试在我的WordPress项目中使用ajax根据其帖子ID动态加载视频。 At the moment when I click on the video link I get the error 500 internal server error. 当我单击视频链接时,出现错误500内部服务器错误。 I send the post ID via ajax to a PHP script, there I attempt to get the post meta of the post using the ID and then display the video url stored within that post meta. 我通过ajax将帖子ID发送到PHP脚本,在那里我尝试使用ID获取帖子的帖子元,然后显示存储在该帖子元中的视频网址。 Can anyone see where I am going wrong? 谁能看到我要去哪里错了?

Below is my shortcode function 下面是我的简码功能

/**
     * Render the HTML code for the  speaker interviews
     *
     * @return $output string The HTML code to be rendered, caught by ob_start();
     */

    function test_view_shortcode_fn( $attributes ) {

        ob_start();

        $args = array( 'post_type' => 'test_video',
                                       'post_status' => 'publish',
                                       'posts_per_page' => 1
                                       );

        $main_video = new WP_Query( $args );

        if( $main_video->have_posts() ) : while ( $main_video->have_posts() ) : $main_video->the_post(); 


        $video = get_post_meta( get_the_ID(), '_video_url' );
        $poster = get_post_meta( get_the_ID(), '_video_poster' );

        ?>
            <div class="row">
                <div class="columns small-12 medium-7 large-8 video-single">
                    <?php echo do_shortcode("[video src='" . $video[0] . "' poster='" . $poster[0] . "' width='800' height='640' preload='none' ]") ?>
                    <ul class="channel-share">
                        <li><a class="text-center" href="https://www.facebook.com/sharer/sharer.php?u=<?php the_permalink(); ?>" title="Share on Facebook" target="_blank"><i class="fa fa-facebook"></i></a></li>
                        <li><a class="text-center" href="https://twitter.com/home?status=<?php the_title(); ?>-<?php the_permalink(); ?>" title="Share on Twitter" target="_blank"><i class="fa fa-twitter"></i></a></li>
                        <li><a class="text-center" href="https://www.linkedin.com/shareArticle?mini=true&url=<?php the_permalink(); ?>&title=<?php the_title(); ?>" title="Share on Linked In" target="_blank"><i class="fa fa-linkedin"></i></a></li>
                        <li><a class="text-center" href="https://plus.google.com/share?url=<?php the_permalink(); ?>" title="Share on Google+" target="_blank"><i class="fa fa-google-plus"></i></a></li>
                    </ul>
                    <?php the_content(); ?>
                </div>
                <div class="columns small-12 medium-5 large-4 video-single">
                    <aside>
                        <ul class="iot-channel-sidebar">
                            <?php

                            $args = array(
                                    'post_type'      => 'test_video',
                                    'posts_per_page' => 5,
                                    'orderby'        => 'rand'
                                );

                            $channel = new WP_Query( $args );

                            if( $channel->have_posts() ) : while( $channel->have_posts() ) : $channel->the_post();
                                $poster = get_post_meta( get_the_ID(), '_video_poster' );
                                $id = get_the_ID();
                                $video_ajax_url = VIDEO_URI . '/inc/video_ajax.php';
                            ?>
                            <?php if( $poster ) { ?>
                                <li class="video-archive-item">
                                    <div class="video-poster">
                                        <a title="<?php the_title(); ?>" id="video_<?php echo $id; ?>">
                                            <img src="<?php echo $poster[0]; ?>" alt="<?php the_title(); ?>" />
                                        </a>
                                    </div>
                                    <div class="video-title">
                                        <a title="<?php the_title(); ?>">
                                            <h4><?php the_title(); ?></h4>
                                        </a>
                                    </div>
                                </li>
                                <script type="text/javascript">
                                        jQuery("#video_<?php echo $id; ?>").click(function(){
                                            jQuery.ajax({
                                                url: '<?php echo $video_ajax_url; ?>',
                                                type: 'POST',
                                                data: {id: '<?php echo $id; ?>'},
                                                    success: function(data){
                                                    $('.wp-video').html(data); // Load data into a <div> as HTML

                                                }
                                            });
                                        });
                                </script
                            <?php } ?>                  
                            <?php endwhile; endif; ?>
                        </ul>
                    </aside>
                </div>
            </div>

        <?php  endwhile; endif;

        $output = ob_get_clean();

        return $output;


    }

My ajax: 我的ajax:

<?php

global $post;
$postID = $_POST['id'];

setup_postdata( $postID ); 

echo $postID;

$video = get_post_meta( $postID, '_video_url' );
$poster = get_post_meta( $postID, '_video_poster' );

echo do_shortcode("[video src='" . $video[0] . "' poster='" . $poster[0] . "' width='800' height='640' preload='none' ]")



?>

I finally realised this was due to the fact that WordPress wasn't being loaded on the PHP file being called via Ajax. 我最终意识到这是由于WordPress并未加载到通过Ajax调用的PHP文件中。

To fix this I included wp-load.php at the top of my PHP script the Ajax was calling and it works perfectly. 为了解决这个问题,我在Ajax调用的PHP脚本的顶部包含了wp-load.php,它可以完美地工作。 Here's what I added to my PHP file. 这是我添加到我的PHP文件中的内容。

<?php
$filename = "../../../../wp-load.php";

include($filename);

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM