500内部服务器错误jQuery和WordPress

I am attempting to use ajax in my WordPress project to dynamically load videos depending on their post ID. At the moment when I click on the video link I get the error 500 internal server error. 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. 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:

<?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' ]")



?>
duanju8308
duanju8308 检查你的错误日志,500来自PHP中的东西。
接近 5 年之前 回复

1个回答

I finally realised this was due to the fact that WordPress wasn't being loaded on the PHP file being called via Ajax.

To fix this I included wp-load.php at the top of my PHP script the Ajax was calling and it works perfectly. Here's what I added to my PHP file.

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

include($filename);

?>
douzhan1031
douzhan1031 你还应该看一下如何正确地使用wordpress进行ajax(官方方式)。 codex.wordpress.org/AJAX_in_Plugins
接近 5 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐