dongpao1921 2015-12-07 14:49
浏览 50
已采纳

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



?>
  • 写回答

1条回答 默认 最新

  • douchen2025 2015-12-07 16:58
    关注

    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);
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集