代码之家  ›  专栏  ›  技术社区  ›  Ali

如何将WordPress模板与CodeIgniter集成

  •  43
  • Ali  · 技术社区  · 15 年前

    如何将codeigner和wordpress集成到 WordPress博客被传送到CodeIgniter创建的页面?

    5 回复  |  直到 7 年前
        1
  •  31
  •   unknowndomain    11 年前

    第一步是将codeigner和wordpress文件移动到它们自己的目录中。

    在那之后,把下面的线放在你的代码点火器的顶部 index.php 文件。将路径更改为 wp-blog-header.php 根据需要指向WordPress的根目录。

    <?php
        require('../wp-blog-header.php');
    

    然后,您可以在视图中使用以下功能:

    <?php
        get_header();
        get_sidebar();
        get_footer();    
    ?>
    

    在WordPress的文档中也可以找到其他帮助器函数,它可以 帮助您集成设计。

        2
  •  16
  •   unknowndomain    11 年前

    当我在codeigner的index.php页面中包含wp-blog-header.php文件时,我遇到了一个问题,即在codeigner的url助手和wordpress中都定义了站点_URL()。我用以下代码解决了这个问题:

    require('blog/wp-blog-header.php');
    
    add_filter('site_url', 'ci_site_url', 1);
    
    function ci_site_url() {
        include(BASEPATH.'application/config/config.php');
        return $config['base_url'];
    }
    
    header("HTTP/1.0 200 OK");
    

    最后一行需要添加,因为WordPress文件正在将HTTP响应头“http/1.0 404 page not found”添加到头中。

    现在可以使用wordpress函数调用codeigntier了。

        3
  •  5
  •   Ram Sharma    9 年前

    这是另一种在代码点火器项目中使用WordPress模板的方法。这对我更好,所以我想和大家分享。使用Wordpress 3.3.1和CodeIgniter 2.1进行测试。

    目录结构:

    / - WordPress
    /ci/ - codeigniter
    

    /ci/index.php(ci索引文件的顶部)

    $wp_did_header = true;
    
    if ( defined('E_RECOVERABLE_ERROR') )
        error_reporting(E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_ERROR |   E_WARNING | E_PARSE | E_USER_ERROR | E_USER_WARNING | E_RECOVERABLE_ERROR);
    else
        error_reporting(E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_ERROR | E_WARNING | E_PARSE | E_USER_ERROR | E_USER_WARNING);
    
    require_once("../wp-config.php");
    

    通过覆盖默认的codeigner版本来处理站点的url函数冲突。你需要改变你使用过的任何地方 site_url() 在代码点火器中使用 ci_site_url() 相反。

    /ci/application/helpers/my_url_helper.php

    <?php
    function anchor($uri = '', $title = '', $attributes = '')
    {
        $title = (string) $title;
    
        if ( ! is_array($uri))
        {
            $site_url = ( ! preg_match('!^\w+://! i', $uri)) ? ci_site_url($uri) : $uri;
        }
        else
        {
            $site_url = ci_site_url($uri);
        }
    
        if ($title == '')
        {
            $title = $site_url;
        }
    
        if ($attributes != '')
        {
            $attributes = _parse_attributes($attributes);
        }
    
        return '<a href="'.$site_url.'"'.$attributes.'>'.$title.'</a>';
    }
    
    
    if ( ! function_exists('ci_site_url'))
    {
        function ci_site_url($uri = '')
        {
            $CI =& get_instance();
            return $CI->config->site_url($uri);
        }
    }
    
    function current_url()
    {
        $CI =& get_instance();
        return $CI->config->ci_site_url($CI->uri->uri_string());
    }
    
    
    function anchor_popup($uri = '', $title = '', $attributes = FALSE)
    {
        $title = (string) $title;
    
        $site_url = ( ! preg_match('!^\w+://! i', $uri)) ? ci_site_url($uri) : $uri;
    
        if ($title == '')
        {
            $title = $site_url;
        }
    
        if ($attributes === FALSE)
        {
            return "<a href='javascript:void(0);' onclick=\"window.open('".$site_url."', '_blank');\">".$title."</a>";
        }
    
        if ( ! is_array($attributes))
        {
            $attributes = array();
        }
    
        foreach (array('width' => '800', 'height' => '600', 'scrollbars' => 'yes', 'status' => 'yes', 'resizable' => 'yes', 'screenx' => '0', 'screeny' => '0', ) as $key => $val)
        {
            $atts[$key] = ( ! isset($attributes[$key])) ? $val : $attributes[$key];
            unset($attributes[$key]);
        }
    
        if ($attributes != '')
        {
            $attributes = _parse_attributes($attributes);
        }
    
        return "<a href='javascript:void(0);' onclick=\"window.open('".$site_url."', '_blank', '"._parse_attributes($atts, TRUE)."');\"$attributes>".$title."</a>";
    }
    
    
    
    function redirect($uri = '', $method = 'location', $http_response_code = 302)
    {
        if ( ! preg_match('#^https?://#i', $uri))
        {
            $uri = ci_site_url($uri);
        }
    
        switch($method)
        {
            case 'refresh'  : header("Refresh:0;url=".$uri);
                break;
            default         : header("Location: ".$uri, TRUE, $http_response_code);
                break;
        }
        exit;
    }
    

    你现在可以使用WordPress了 get_header() 和/或 get_footer() 用于在CI项目中绘制模板的函数。

        4
  •  3
  •   Chris Aelbrecht    10 年前

    我正在使用WordPress管理一个定制的CI电子商务网站中的文章。CI是我的主要网站。目录结构如下:

     /application (CI)
     /... (directories like javascript, stylesheets ...)
     /system (CI)
     /wordpress
     /.htaccess
     /index.php (CI)
    

    在将以下代码添加到CI的顶部时,我可以在CI控制器中使用Wordpress函数,而不会将URL弄乱 索引文件 :

    require_once './wordpress/wp-blog-header.php';
    
    add_filter('site_url', 'ci_site_url', 1);
    
    function ci_site_url($uri = '') {
        $CI =& get_instance();
        $uri = ltrim(str_replace($CI->config->base_url('wordpress/'), '', $uri),'/'); // "wordpress/" is in my case the name of the directory where I installed Wordpress. See directory structure above.
        return $CI->config->site_url($uri);
    }
    

    也适用于使用J_?R_?Me Jaglale的CI I18N库( http://jeromejaglale.com/doc/php/codeigniter_i18n )

        5
  •  0
  •   Eric    13 年前

    如果您计划在代码中使用代码点火器站点的url函数,或者如果您正在合并现有的CI站点和wp…这可能会有所帮助:

    在ci index.php的顶部:

    require_once '../wp-blog-header.php';
    
    add_filter('site_url', 'ci_site_url', 4);
    
    function ci_site_url($url, $path, $orig_scheme, $blog_id) {
        $CI =& get_instance();
        $new_path = str_replace("YOURSITEURLGOESHERE", "", $url);
        return  $CI->config->site_url($new_path);
    }
    

    实际上,这允许您在CI中使用网站URL,因此如果您已经向项目添加了大量链接和内容,它可能会帮助您解决问题。